Home Business Intelligence Creating Customized Desk in SSAS Tabular utilizing Desk and Row Constructors in DAX

Creating Customized Desk in SSAS Tabular utilizing Desk and Row Constructors in DAX

0
Creating Customized Desk in SSAS Tabular utilizing Desk and Row Constructors in DAX

[ad_1]

Dynamic_Measures_in_Card_Visual_-_Power_BI_Desktop

Some time in the past I used to be engaged on a Energy BI challenge which the shopper wished to outline a brand new desk immediately within the mannequin. The best method to obtain this in Energy BI Desktop is to “Enter Knowledge” which creates a brand new desk by typing or pasting in new contents. I used to be pondering of that challenge the opposite day and thought, hey, how we are able to do the identical in SSAS Tabular when there isn’t a Energy Question (M) language accessible in SSAS Tabular 2016. The excellent news is that Energy Question will probably be accessible within the subsequent model of SSAS Tabular in SQL Server vNext. However, till then a workaround can be coming into knowledge to a CSV file then load it to the mannequin. One other method is to implement customized tables in DAX in SQL Server 2016 Tabular fashions utilizing Desk and Row Constructors. On this publish I present you a method of making customized desk in SSAS Tabular utilizing desk constructors in DAX. You are able to do the identical in Energy BI as the identical precept applies. Subsequently, in case you’d favor to not use “Enter Knowledge” characteristic which successfully makes use of Energy Question to create a brand new desk in Energy BI Desktop, then you need to use DAX to do the identical.

If don’t have already got SQL Server 2016 it’s most likely time to obtain and set up it. I exploit AdventureWorksDW as pattern database on this article.

You might be concerned with an SSAS Tabular challenge and the shopper requested for a report in Energy BI with dynamic Card in order that the values proven within the Card visible ought to dynamically change based mostly on chosen measure from a slicer. You might have a number of totally different measures within the mannequin and the shopper needs to point out a few of them dynamically in just one Card visible. Think about you might have the next measures to be proven within the Card:

  • Whole Web Gross sales
  • Web Gross sales in 2014
  • Whole Variety of Web Gross sales Transactions

It’s a must to create a logic in order that the customers can chosen any of the above measures to point out in a single Card visible.

After you meet the necessities, you’re good to start out implementing the above situation in SQL Server Knowledge Instrument (SSDT). Making a calculated desk in SSAS Tabular 2016 is pretty simple. All we have to do is to create a customized desk with two columns. One column shops pleasant names for measures and the opposite one holds DAX expressions for the measures. As you may need seen, I’m speaking about making a customized desk in DAX and populating it with values. Proceed studying to see how. What we’re going to do is to create a calculated desk utilizing desk constructors in DAX. Desk and Row Constructors weren’t accessible in earlier variations of DAX in SSAS Tabular. They’re similar to Lists or an inventory of Tuples identical to what we now have in MDX.

I’ll clarify this later after we created our pattern mannequin in SSDT.

Create a New SSAS Tabular Challenge in SSDT

As I acknowledged earlier than SSAS Tabular 2016 is required to implement the next methodology. The reason being that we now have to create a calculated desk in our mannequin and calculated desk is NOT accessible in prior variations of SSAS Tabular.

  • Open SSDT
  • Create a brand new SSAS Tabular challenge
  • Click on “Built-in workspace” and click on OK
  • Click on “Mannequin” menu then click on “Import from Knowledge Supply”
  • Enter SQL Server title then choose AdventureWorksDW and click on Subsequent
  • Enter “Impersonation Data” then click on Subsequent
  • Persist with the default then click on Subsequent
  • Choose “FactInternetSales” and “DimDate” then click on End

Up to now we created an SSAS Tabular challenge in SSDT 2015 and loaded FactInternetSales to the mannequin.

Create Wanted Measures
On this situation we have to create 3 new measures as follows:

Whole Web Gross sales := SUM ( 'FactInternetSales'[SalesAmount] )
Web Gross sales In 2014 := CALCULATE(SUM( 'FactInternetSales'[SalesAmount] ), 'DimDate'[CalendarYear] = 2014)
Whole Variety of Web Gross sales Transactions := COUNTROWS ( 'FactInternetSales' )

Create a Customized Desk Utilizing Desk and Row Constructors DAX

Making a Calculated Desk in SSDT is comparatively simple. We simply have to click on the corresponding button and create a brand new desk utilizing desk and row constructors. Let me clarify Desk and Row Constructors just a little bit.

Desk Constructor in DAX

Desk constructor is a brand new characteristic in DAX which is basically cool. To assemble a desk, we have to put the values in curly brackets.  As an example, to create a desk of “Blue”, Orange”, “Purple” we simply want to write down the next:

{“Blue”, “Orange”, “Purple”}

To see the way it works in DAX:

  • In SSDT, proper click on the mannequin from “Answer Explorer” and click on “Properties”
  • Copy workspace server from SSDT
  • Open SQL Server Administration Studio 2016 (SSMS)
  • Hook up with the native occasion of SSAS Tabular by pasting the workspace server Handle you copied from SSDT
  • Open a brand new MDX question and run the next DAX question
EVALUATE
    {"RED", "BLUE","ORANGE"}

Sure, you ran a DAX question in an MDX question editor as no DAX question editor at present accessible in SSMS.

The consequence exhibits a desk of Blue, Orange and Purple.

Row Constructor in DAX

To assemble a row, we are able to put the values in parenthesis after which encapsulate the entire in curly brackets like {(“Blue”, Orange”, “Purple”)}. Run the next DAX question in SSMS to see the outcomes:

EVALUATE
    {("Blue", "Orange", "Purple")}

Combining Desk and Row Constructors in DAX

Now that you just obtained the concept how one can assemble a desk  or a row, it’s simple to mix these two collectively. Run the next DAX question in SSMS.

EVALUATE
    {("Banana", "Yellow"), ("Cucumber", "Inexperienced")}

As you possibly can see, there are column names which added mechanically. However, they aren’t actually helpful. So let’s put some significant names on the columns. We are able to simply use SELECTCOLUMNS() operate in DAX to attain this. Run the next DAX question and see the outcomes:

EVALUATE
    SELECTCOLUMNS(
       {("Banana", "Yellow"), ("Cucumber", "Inexperienced")}
       , "Fruit Identify", [Value1]
       , "Color", [Value2]
       )

Sensible.

Now let’s implement our situation utilizing the above method. What we have to do is to create a customized desk containing measure names and the precise measures themselves. Copy, paste and run the next DAX question in SSMS:

It’s attention-grabbing isn’t it? What the above question does is that it runs the specific measures we outlined earlier and put the leads to a desk.

The following step is to rename the columns utilizing SELECTCOLUMNS(). The question under does the job:

Let’s get again to our answer in SSDT and create a calculated desk utilizing the above method.

Creating Calculated Desk in SSAS Tabular

  • Change again to the SSAS tabular answer in SSDT and click on to create a calculated desk
  • Copy and paste the next DAX expression
=SELECTCOLUMNS(
             {("Whole Web Gross sales", [Total Internet Sales])
             , ("Web Gross sales In 2014", [Internet Sales In 2014])
             , ("Whole Variety of Web Gross sales Transactions", [Total Number of Internet Sales Transactions])}
             , "Measure Identify", [Value1]
             , "Measure Outcomes", [Value2]
             )
  • You’ll be able to rename the calculated desk by double clicking within the desk tab

Up to now we efficiently created a calculated desk containing our measure names and their values.

Bear in mind: We supposed to point out dynamic measures in a single Card visible within the out visualisation. The presentation layer may be Energy BI, Excel and so forth.

To have the ability to obtain the aim of displaying the measures dynamically we have to create a brand new measure which principally exhibits the values of a specific measure by the top consumer.

  • Create a brand new measure and duplicate/paste the next DAX expression:
Dynamic Measure:= CALCULATE(VALUES('Dynamic Card'[Measure Results])
            , filter(ALLSELECTED('Dynamic Card'[Measure Name])
                , 'Dynamic Card'[Measure Name]=[Measure Name]))

You’ll get the next error instantly after you create the brand new measure:

“MdxScript(Mannequin) (1, 59) Calculation error in measure ‘Dynamic Card'[Dynamic Measure]: A desk of a number of values was provided the place a single worth was anticipated.”

The explanation we get the above error message is that we’re passing a number of values to the measure fairly than a single worth within the filter half. We are able to put a relentless worth within the filter, however, it’s not what we wish. Trying on the DAX expression you’ll shortly see that we’ve used ALLSELECTED() to filter the values based mostly on regardless of the finish consumer selects within the report. So we have to put a Slicer on the report later. So don’t fear in regards to the error message. However, keep in mind that the consumer ought to choose just one worth from the Slicer, so we’ll have to setup the Slicer as single choose.

Let’s browse our SSAS Tabular mannequin in Excel.

Looking SSAS Tabular in Excel

You’ll be able to simply browse the mannequin in Excel to check the performance we’re on the lookout for.

  • Click on on the Excel button on SSDT to open Excel and browse the mannequin

Word: You’ll have to have Microsoft Excel put in in your machine to have the ability to browse your mannequin in Excel.

  • Click on OK on the “Analyse in Excel” window
  • Click on “Dynamic Measure”
  • You’ll instantly get an error message
  • Proper click on “Measure Identify” from fields record and click on “Add as Slicer”
  • Now choose an merchandise within the slicer

And that is what you get… Success!

Hook up with SSAS Tabular from Energy BI

  • Open Energy BI Desktop
  • Get knowledge from SQL Server Evaluation Companies
  • Use workspace server from SSDT as server title (you learnt how to try this earlier on this article. In brief, you will discover it within the mannequin properties)
  • Dwell connect with the mannequin
  • Put a automotive visible on the report and tick “Dynamic Measures”
  • You’ll get an error message, that
  • Add a Slicer to the report then choose “Measure Identify”
  • Choose an Merchandise from the Slicer

Formatting the Dynamic Measures

Up to now we achieved the aim, however, as you possibly can see the consequence is just not that informative and helpful. When you choose totally different objects from the slicer the worth proven within the Card visible is just not that informative by itself. So let’s add some formatting to the “Dynamic Measure”. To take action, we have to modify the DAX expression we used to assemble our calculated desk.

  • Change again to SSDT
  • Click on “Dynamic Card” tab and modify the DAX expression as under
=  SELECTCOLUMNS(             {("Whole Web Gross sales", FORMAT([Total Internet Sales], "Forex"))           , ("Web Gross sales In 2014", FORMAT([Internet Sales In 2014], "Forex"))              , ("Whole Variety of Web Gross sales Transactions", FORMAT([Total Number of Internet Sales Transactions] ,"0,0"))}            , "Measure Identify", [Value1]              , "Measure Outcomes", [Value2]           )
  • Change again to Energy BI Desktop and refresh knowledge
  • That appears good, however, when you’ve got OCD like me you then’d favor to make use of the next DAX expression which provides some textual content to the measure to make it self explanatory
  • Refresh knowledge once more in Energy BI Desktop and here’s what you get
  • Set “Phrase wrap” to “On” for Card visible

The job is finished!

This may my final publish in 2016, so I want you all an exquisite and joyful Christmas.

See you subsequent 12 months!

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here