Home Business Intelligence Datatype Conversion in Energy Question Impacts Knowledge Modeling in Energy BI

Datatype Conversion in Energy Question Impacts Knowledge Modeling in Energy BI

0
Datatype Conversion in Energy Question Impacts Knowledge Modeling in Energy BI

[ad_1]

Datatype Conversion in Power Query Affects Data Modeling in Power BI

In my consulting expertise working with prospects utilizing Energy BI, many challenges that Energy BI builders face are on account of negligence to information sorts. Listed here are some frequent challenges which are the direct or oblique outcomes of inappropriate information sorts and information kind conversion:

  • Getting incorrect outcomes whereas all calculations in your information mannequin are appropriate.
  • Poor performing information mannequin.
  • Bloated mannequin measurement.
  • Difficulties in configuring user-defined aggregations (agg consciousness).
  • Difficulties in establishing incremental information refresh.
  • Getting clean visuals after the primary information refresh in Energy BI service.

On this blogpost, I clarify the frequent pitfalls to forestall future challenges that may be time-consuming to determine and repair.

Background

Earlier than we dive into the subject of this weblog put up, I wish to begin with a little bit of background. Everyone knows that Energy BI isn’t solely a reporting instrument. It’s certainly a knowledge platform supporting numerous features of enterprise intelligence, information engineering, and information science. There are two languages we should be taught to have the ability to work with Energy BI: Energy Question (M) and DAX. The aim of the 2 languages is sort of completely different. We use Energy Question for information transformation and information preparation, whereas DAX is used for information evaluation within the Tabular information mannequin. Right here is the purpose, the 2 languages in Energy BI have completely different information sorts.

The most typical Energy BI improvement eventualities begin with connecting to the info supply(s). Energy BI helps tons of of information sources. Most information supply connections occur in Energy Question (the info preparation layer in a Energy BI answer) except we join reside to a semantic layer reminiscent of an SSAS occasion or a Energy BI dataset. Many supported information sources have their very own information sorts, and a few don’t. As an example, SQL Server has its personal information sorts, however CSV doesn’t. When the info supply has information sorts, the mashup engine tries to determine information sorts to the closest information kind obtainable in Energy Question. Although the supply system has information sorts, the info sorts won’t be suitable with Energy Question information sorts. For the info sources that don’t help information sorts, the matchup engine tries to detect the info sorts primarily based on the pattern information loaded into the info preview pane within the Energy Question Editor window. However, there isn’t any assure that the detected information sorts are appropriate. So, it’s best observe to validate the detected information sorts anyway.

Energy BI makes use of the Tabular mannequin information sorts when it hundreds the info into the info mannequin. The information sorts within the information mannequin might or might not be suitable with the info sorts outlined in Energy Question. As an example, Energy Question has a Binary information kind, however the Tabular mannequin doesn’t.

The next desk reveals Energy Question’s datatypes, their representations within the Energy Question Editor’s UI, their mapping information sorts within the information mannequin (DAX), and the inner information sorts within the xVelocity (Tabular mannequin) engine:

Power Query and DAX (data model) data type mapping
Energy Question and DAX (information mannequin) information kind mapping

Because the above desk reveals, in Energy Question’s UI, Entire Quantity, Decimal, Fastened Decimal and Proportion are all in kind quantity within the Energy Question engine. The sort names within the Energy BI UI additionally differ from their equivalents within the xVelocity engine. Allow us to dig deeper.

Knowledge Varieties in Energy Question

As talked about earlier, in Energy Question, we have now just one numeric datatype: quantity whereas within the Energy Question Editor’s UI, within the Remodel tab, there’s a Knowledge Sort drop-down button displaying 4 numeric datatypes, as the next picture reveals:

Data type representations in the Power Query Editor's UI
Knowledge kind representations within the Energy Question Editor’s UI

In Energy Question components language, we specify a numeric information kind as kind quantity or Quantity.Sort. Allow us to have a look at an instance to see what this implies.

The next expression creates a desk with completely different values:

#desk({"Worth"}
	, {
		{100}
		, {65565}
		, {-100000}
		, {-999.9999}
		, {0.001}
		, {10000000.0000001}
		, {999999999999999999.999999999999999999}
		, {#datetimezone(2023,1,1,11,45,54,+12,0)}
		, {#datetime(2023,1,1,11,45,54)}
		, {#date(2023,1,1)}
		, {#time(11,45,54)}
		, {true}
		, {#length(11,45,54,22)}
		, {"This can be a textual content"}
	})

The outcomes are proven within the following picture:

Generating values in Power Query
Producing values in Energy Question

Now we add a brand new column that reveals the info kind of the values. To take action, use the Worth.Sort([Value]) operate returns the kind of every worth of the Worth column. The outcomes are proven within the following picture:

Getting a column's value types in Power Query
Getting a column’s worth sorts in Energy Question

To see the precise kind, we must click on on every cell (not the values) of the Worth Sort column, as proven within the following picture:

Click on a cell to see its type in Power Query Editor
Click on on a cell to see its kind in Energy Question Editor

With this methodology, we have now to click on every cell in to see the info varieties of the values that isn’t preferrred. However there’s presently no operate obtainable in Energy Question to transform a Sort worth to Textual content. So, to indicate every kind’s worth as textual content in a desk, we use a easy trick. There’s a operate in Energy Question returning the desk’s metadata: Desk.Schema(desk as desk). The operate ends in a desk revealing helpful details about the desk used within the operate, together with column TitleTypeNameForm, and so forth. We need to present TypeName of the Worth Sort column. So, we solely want to show every worth right into a desk utilizing the Desk.FromValue(worth as any) operate. We then get the values of the Form column from the output of the Desk.Schema() operate.

To take action, we add a brand new column to get textual values from the Form column. We named the brand new column Datatypes. The next expression caters to that:

Desk.Schema(
      Desk.FromValue([Value])
      )[Kind]{0}

The next picture reveals the outcomes:

Getting type values as text in Power Query
Getting kind values as textual content in Energy Question

Because the outcomes present, all numeric values are of kind quantity and the best way they’re represented within the Energy Question Editor’s UI doesn’t have an effect on how the Energy Question engine treats these sorts. The information kind representations within the Energy Question UI are someway aligned with the sort aspects in Energy Question. A side is used so as to add particulars to a kind sort. As an example, we are able to use aspects to a textual content kind if we need to have a textual content kind that doesn’t settle for null. We are able to outline the worth’s sorts utilizing kind aspects utilizing Aspect.Sort syntax, reminiscent of utilizing In64.Sort for a 64-bit integer quantity or utilizing Proportion.Sort to indicate a quantity in share. Nonetheless, to outline the worth’s kind, we use the kind typename syntax reminiscent of defining quantity utilizing kind quantity or a textual content utilizing kind textual content. The next desk reveals the Energy Question sorts and the syntax to make use of to outline them:

Defining types and facets in Power Query M
Defining sorts and aspects in Energy Question M

Sadly, the Energy Question Language Specification documentation doesn’t embrace aspects and there usually are not many on-line assets or books that I can reference right here apart from Ben Gribaudo’s weblog who completely defined aspects intimately which I strongly suggest studying.

Whereas Energy Question engine treats the values primarily based on their sorts not their aspects, utilizing aspects is beneficial as they have an effect on the info when it’s being loaded into the info mannequin which raises a query: what occurs after we load the info into the info mannequin? which brings us to the subsequent part of this weblog put up.

Knowledge sorts in Energy BI information mannequin

Energy BI makes use of the xVelocity in-memory information processing engine to course of the info. The xVelocity engine makes use of columnstore indexing expertise that compresses the info primarily based on the cardinality of the column, which brings us to a important level: though the Energy Question engine treats all of the numeric values as the sort quantity, they get compressed in another way relying on their column cardinality after loading the values within the Energy BI mannequin. Due to this fact, setting the proper kind side for every column is necessary.

The numeric values are one of the frequent datatypes utilized in Energy BI. Right here is one other instance displaying the variations between the 4 quantity aspects. Run the next expression in a brand new clean question within the Energy Question Editor:

// Decimal Numbers with 6 Decimal Digits
let
    Supply = Checklist.Generate(()=> 0.000001, every _ <= 10, every _ + 0.000001 ),
    #"Transformed to Desk" = Desk.FromList(Supply, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Renamed Columns" = Desk.RenameColumns(#"Transformed to Desk",{{"Column1", "Supply"}}),
    #"Duplicated Supply Column as Decimal" = Desk.DuplicateColumn(#"Renamed Columns", "Supply", "Decimal", Decimal.Sort),
    #"Duplicated Supply Column as Fastened Decimal" = Desk.DuplicateColumn(#"Duplicated Supply Column as Decimal", "Supply", "Fastened Decimal", Foreign money.Sort),
    #"Duplicated Supply Column as Proportion" = Desk.DuplicateColumn(#"Duplicated Supply Column as Fastened Decimal", "Supply", "Proportion", Proportion.Sort)
in
    #"Duplicated Supply Column as Proportion"

The above expressions create 10 million rows of decimal values between 0 and 10. The ensuing desk has 4 columns containing the identical information with completely different aspects. The primary column, Supply, comprises the values of kind any, which interprets to kind textual content. The remaining three columns are duplicated from the Supply column with completely different kind aspects, as follows:

  • Decimal
  • Fastened decimal
  • Proportion

The next screenshot reveals the ensuing pattern information of our expression within the Energy Question Editor:

Generating 10 million numeric values and use different type facets in Power Query M
Producing 10 million numeric values and use completely different kind aspects in Energy Question M

Now click on Shut & Apply from the Dwelling tab of the Energy Question Editor to import the info into the info mannequin. At this level, we have to use a third-party neighborhood instrument, DAX Studio, which will be downloaded from right here.

After downloading and putting in, DAX Studio registers itself as an Exterior Software within the Energy BI Desktop as the next picture reveals:

External tools in Power BI Desktop
Exterior instruments in Energy BI Desktop

Click on the DAX Studio from the Exterior Instruments tab which robotically connects it to the present Energy BI Desktop mannequin, and observe these steps:

  1. Click on the Superior tab
  2. Click on the View Metrics button
  3. Click on Columns from the VertiPaq Analyzer part
  4. Have a look at the CardinalityCol Dimension, and % Desk columns

The next picture reveals the previous steps:

VertiPaq Analyzer Metrics in DAX Studio
VertiPaq Analyzer Metrics in DAX Studio

The outcomes present that the Decimal column and Proportion consumed essentially the most vital a part of the desk’s quantity. Their cardinality can also be a lot greater than the Fastened Decimal column. So right here it’s now extra apparent that utilizing the Fastened Decimal datatype (side) for numeric values might help with information compression, decreasing the info mannequin measurement and growing the efficiency. Due to this fact, it’s clever to all the time use Fastened Decimal for decimal values. Because the Fastened Decimal values translate to the Foreign money datatype in DAX, we should change the columns’ format if Foreign money is unsuitable. Because the title suggests, Fastened Decimal has fastened 4 decimal factors. Due to this fact, if the unique worth has extra decimal digits after conversion to the Fastened Decimal, the digits after the fourth decimal level will likely be truncated.

That’s the reason the Cardinality column within the VertiPaq Analyzer in DAX Studio reveals a lot decrease cardinality for the Fastened Decimal column (the column values solely maintain as much as 4 decimal factors, no more).

Obtain the pattern file from right here.

So, the message is right here to all the time use the datatype that is sensible to the enterprise and is environment friendly within the information mannequin. Utilizing the VertiPaq Analyzer in DAX Studio is sweet for understanding the varied features of the info mannequin, together with the column datatypes. As a knowledge modeler, it’s important to grasp how the Energy Question sorts and aspects translate to DAX datatypes. As we noticed on this weblog put up, information kind conversion can have an effect on the info mannequin’s compression fee and efficiency.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here