Named Sets Archive | THE SELF-SERVICE-BI BLOG Wir lieben Microsoft Power BI Fri, 22 Mar 2024 16:26:30 +0000 de hourly 1 https://wordpress.org/?v=6.7.2 https://ssbi-blog.de/wp-content/uploads/2019/10/Favicon-150x150.png Named Sets Archive | THE SELF-SERVICE-BI BLOG 32 32 How to control Named Sets via Slicer using MDX https://ssbi-blog.de/blog/technical-topics-english/how-to-control-named-sets-via-slicer-using-mdx/ https://ssbi-blog.de/blog/technical-topics-english/how-to-control-named-sets-via-slicer-using-mdx/#comments Fri, 26 Sep 2014 18:00:49 +0000 http://ssbi-company.de/?p=414 In my last post How to build asymmetric pivots on Power Pivot data models I showed you how to build asymmetric reports with Power Pivot, using Named Sets. The result of that post was a Named Set, which you had to choose from the PivotTable fields list manually. This post is about how not to select a […]

Der Beitrag How to control Named Sets via Slicer using MDX erschien zuerst auf THE SELF-SERVICE-BI BLOG.

]]>
In my last post How to build asymmetric pivots on Power Pivot data models I showed you how to build asymmetric reports with Power Pivot, using Named Sets. The result of that post was a Named Set, which you had to choose from the PivotTable fields list manually. This post is about how not to select a new Named Sets from the PivotTable fields list, each time you want to change the period, but how to use slicers to take control over the choosen Named Sets.

How to choose Named Sets manually by PivotTable fields list
How to choose Named Sets manually by PivotTable fields list

The goal of this post is, to improve the selection of the Named Set, you want to see in your pivot table. For doing that you first have to understand what really stands behind Named Sets. Named Sets are defined in MDX (Multidimensional eXpressions), a language to query OLAP cubes. Even when you used the wizard to create your first Named Set, you created an MDX statement. I have absolutely no clue about MDX, but Chris Webb published a series of posts, named Introduction to MDX for PowerPivot Users and I can only recommend to read it for deeper understanding of the topic.

Now let’s see, how you can see the MDX behind your Named Sets.

 

Finding the MDX behind Named Sets

To see the MDX-Statements behind the Named Sets you created with the wizard, follow these steps:

  1. Click into the pivot table
  2. Select the context sensitive menu PivotTable Tool
  3. Click on Tab Analyze
  4. Choose Manage Sets
See the MDX behind Named Sets in Excel - Step01
See the MDX behind Named Sets in Excel – Step01

 

The next windows, that opens is the Set Manager. Select the Named Set you want (1) to see the MDX statement and then click in Edit (2).

See the MDX behind Named Sets in Excel - Step02
See the MDX behind Named Sets in Excel – Step02

The following windows is already your good friend. Here you created all the Named Sets so far. But have you noticed the little button Edit MDX on the lower right? Click on it (1) and confirm the appearing warning with OK. Then press OK (2) in this window.

See the MDX behind Named Sets in Excel - Step03
See the MDX behind Named Sets in Excel – Step03

After confirming with OK you see the following MDX statement.

See the MDX behind Named Sets in Excel - Step04
See the MDX behind Named Sets in Excel – Step04

What does this statement mean. As I sayed before: For getting an perfectly technical background on MDX for PowerPivot read Chris‘ posts. I will keep it less technical . See the following figure.

 

The MDX statement

Because we created all the Named Sets on columns (not on rows) each line (except line 1 and 7) in the MDX statement represents a column in the pivot table. Let’s focus on line 2. This line consists of a selected month (in this case January 2003) and a measure (MIsoActYear_TotalDue = TotalDue of the isolated January in Year 2003).

Now, that we know how a Named Set looks in MDX, there is one more question to overcome: How can we make this Named Set react on a slicer?

 

How to make Named Sets react on slicers

The good news is, that the xVelocity engine, which stands behind Power Pivot, understands both: MDX and DAX 🙂 This is something I learned from Idan Cohen’s post about Dynamic Chart in Power Pivot on Powerpivotpro.com. And this is where the naming convention of your Named Sets comes back into the game. I want to do the following steps:

  1. Building a parameter table in the Power Pivot data model, that includes a column with all the names of the Named Sets (e.g. 200301, 200302, 200303, …, 200512)
  2. Creating a slicer based on that parameter table
  3. Creating a DAX measure, that retrieves the single selected value in the slicer (the selected name of the Named Set)
  4. Creating an MDX statement, that reacts on the written DAX measure

These four steps make it happen, that you can control your Named Sets by slicer. Let’s move on 🙂

 

Build a parameter table in the Power Pivot data model

For being able to control Named Sets via Slicer, we first need to create a parameter table. ‚Parameter table‘ is another word for disconnected table, which is a single table in the data model, that has no relation to other tables. Our table will have two columns:

a) Column ‚Caption‘: This column will be shown in the slicer and will let the user select the year-month-combination.

b) Column ‚YYYYMM‘: This column will show the year-month-combination as number. This will be read by the Named Set and will change the pivot structure accordingly.

Let’s take a look at how it works.

I open a new excel sheet within the same file. The data looks like this:

Building a parameter table
Building a parameter table

You need to ensure, that in this table are only those months represented, for which you already have created a Named Set. For example: We created two Named Sets. One for March 2003 and one for April 2003. Only those two months appear in this table now. This is important for the MDX-statement, which will let us control the Named Sets by Slicer. When you will create further Named Sets in the future, it will be pretty easy to enlarge the parameter table within seconds. I will show later, how that works.

Now we convert these two columns into a table the following way:

 

Converting columns into table
Converting columns into table

The advantage of going this way is, that you can change the table name, before you add that table to the data model. That way the name of your table in Excel and the name of the table in the data model are equal. This is not strictly required, but it’s pretty usefull. Change the name under ‚Table Name‘ (Step 2) in what ever you like. I use ‚ParameterTable‘ for this example.

Changing the table name
Changing the table name

 

After we renamed the table name, we can now go and add that table to the data model.

Adding the parameter table to the data model
Adding the parameter table to the data model

The result in the data model looks like the following:

Data model with parameter table

Data model with parameter tableNow that we’ve got the newly added table in our data model, we can now create a slicer using this table.

Creating a slicer based on the parameter table

Because we want to be able to control a Named Set (which allows us an asymmetric report in a pivot table) via Slicer, we created a (not connected) parameter table to the data model. It is important that you use a disconnected table, because we don’t want other tables in the data model to be filtered, when we select something from the slicer. We only want to be able to define the structure of the pivot table. Therefore the table used for the slicer musn’t have any relations to other tables.

Creating a slicer on that disconnected table works as follows:

 Adding a slicer to the pivot table
Adding a slicer to the pivot table

For adding a slicer to the pivot table click somewhere into the pivot table (1), click on the context sensitive menu PivotTable Tools (2) and on Analyze (3). Clicking on Insert Slicer (4) opens a window, which lets you see the Active and All tables of the data model. Choose tab All (5) and you will be able to select column Caption from the newly added parameter table. Confirm with OK (7). The result can be seen in the following figure:

 

Pivot with slicer unsorted
Pivot with slicer unsorted

What you can see on the first sight is, that the months are sorted alphabetically, what is not usefull at all. Let’s go back to the data model and change this.

 

Changing sort order
Changing sort order

For changing the sort order of the  month in the slicer, you have to change the sort order for the column ‚Caption‘ in the table of the data model. Click on the column you want to have sorted (‚Caption‘) and go to Tab ‚Home‘ (1) and click on ‚Sort by Column‘ (2). Then choose the column ‚YYYYMM‘ as column you want your column ‚Caption‘ get sorted by. Take care, that the column ‚YYYYMM‘ has a numeric data type. Otherwise you will get problems with sorting. Having that done, the slicers shows the correct sort order.

 

Slicer with correct sort order
Slicer with correct sort order

You will notice very quickly, that selection anything from the slicer will have no effect on the pivot table at all. The reason ist, that there is not relationship between the parameter table and any other table of the data model. So this behavior is exactely what we wanted. But what do we do with a slicer, that does not have any effect on the pivot table we want to change the structure? Stay patient, we are coming closer.

 

Creating the DAX measure, that reads slicer selection

Okay, the pivot table doesn’t get the information, which items where selected from the slicer. But we can evaluate the selection from the slicer using DAX. We can write a measure, that looks at the selection, evaluates if only one single item is selected (only one month at the time) and then give the selected item back. The following DAX statement does exactely what we want:

=
MIN ( ParameterTable[YYYYMM] )

DAX Formatter by SQLBI

If there is only one item selected from the slicer, the MIN() function will return the selected item of column [YYYYMM], because if it’s the only one, it’s the minimum at the same tim. If there are more items selected, the MIN() function will return the minimum of those.

A good way to see, what this measure returns in relation to the slicer selection, is a very short cube-function in Excel:

Cube function checking slicer selection
Cube function checking slicer selection

The CUBEVALUE function has three parameters.

  1. Connection: The formula connects to the power pivot data model and its tables, relations and measures.
  2. Measure: We define, that we want to get the values of the measure MSlicerSelection.
  3. Slicers: This is the name of of our slicer Caption, which filters the result of the CUBEVALUE function.

Now we know which items was selected, by using a DAX-Measure. The next step is, to pass this information to a MDX statement, that defines the structure of our pivot table. That way we don’t have to choose the wanted Named Set from the pivot table fields list, but can choose it via slicer, without even knowing, that there is a Named Set behind all that magic 🙂 Let’s go.

Modifying the MDX statement, to read the DAX measure

Before we connect the Named Sets to our Slicer we have to remove the selected Named set from the pivot table.

Remove Named Set from Pivot
Remove Named Set from Pivot

Now we can move on to create a new Named Set using MDX. First go to „Manage Sets“.

Creating Central named Set
Creating Central named Set

Then choose „New…“ (1) and „Create Set using MDX“ (2).

Selecting the MDX editor
Selecting the MDX editor

First give that Named Set a appropriate name (1). For keeping the overview over your Named Sets it’s good to put it in the folder of the other Named Sets. Unfortunately there is no dropdown to choose the folder name from. While writing take care to type it correctly (2). In the next step (3) you define the MDX statement the following way:

Writing the central Named Set with MDX
Writing the central Named Set with MDX

The StringToSet function (STRTOSET) converts a string into a set. I don’t have a clue about MDX. So there will be several ways to do that better. But using the LEFT function around my DAX measure, which reads the slicer selection, creates a string out of a number. This string is used by the STRTOSET function. So, if you select 200303 on the slicer, this selection will be converted into a string by the LEFT function. This string „200303“ will be converted into a set by the STRTOSET function. That way you connect your slicer with your created sets on a dynamic way.

And what happens next month, when a new month has to be added? There are 3 steps to be done:

  1. Create a new Named Set, e.g. 200305
  2. Add a new row to the parameter table (in Excel): Caption May 03, YYYYMM 200305
  3. Resize the parameter table in Excel and refresh the data model

After that you are able to select the new month from the slicer and see another set in your pivot table.

BE CAREFULL: Always do it this way! First create the Named Set, then add a new line to the parameter table. Excel crushes, if you add the line to the parametert able first, because the STRTOSET function will be looking for a Named Set, which doesn’t exists yet. Always create the Named Set first and add the line to the parameter table afterwards!!!

Wooo, that was a long journey. Thanks for staying with me for so long. Now you have created a connection between a slicer on a Power Pivot column and one MDX statement, that dynamically returns a specific Named Set. Look at the result:

The final result: Using Slicers to control Named Sets
The final result: Using Slicers to control Named Sets

See you next time and keep in mind: Sharing is caring. If you liked this articel, feel free to share it 

Greets from Hamburg,

Lars

Der Beitrag How to control Named Sets via Slicer using MDX erschien zuerst auf THE SELF-SERVICE-BI BLOG.

]]>
https://ssbi-blog.de/blog/technical-topics-english/how-to-control-named-sets-via-slicer-using-mdx/feed/ 5
How to build asymmetric pivots on Power Pivot data models using Named Sets https://ssbi-blog.de/blog/technical-topics-english/how-to-build-asymmetric-pivots-on-power-pivot-data-models-using-named-sets/ Sun, 21 Sep 2014 18:00:43 +0000 http://ssbi-company.de/?p=127 There are many financial reports out there, which have an asymmetric design, to give you all the numbers you need, for a certain place in time. Take a look at the following example to see, what I am talking about. This report gives an overview of Total Due by Territory, for March 2003. The columns in red square represent the single […]

Der Beitrag How to build asymmetric pivots on Power Pivot data models using Named Sets erschien zuerst auf THE SELF-SERVICE-BI BLOG.

]]>
There are many financial reports out there, which have an asymmetric design, to give you all the numbers you need, for a certain place in time. Take a look at the following example to see, what I am talking about. This report gives an overview of Total Due by Territory, for March 2003. The columns in red square represent the single months for year 2003, with its realized Total Due, until March. In green square you see the year-to-date value for 2003, which is the sum of all three single months. In orange you see a year-to-date value again, but this time it’s for the realized Total Due for the same period in the previous year. This is an asymmetric report design and you cannot do this with a „normal“ pivot table. But I have good news for you: You can build asymmetric pivots on Power Pivot data models and I will show you how, in this post.

 

Asymmetric Reporting of Total Due
Asymmetric pivot table design of Total Due

 

Why ’normal‘ pivots can’t be asymmetric

When we are talking about asymmetric pivot table design, why is this so special? Can’t normal pivots do that? The answer is: No, they can’t. Take a look at the following screenshot to understand why they can’t.

Why normal pivots cannot be asymmetric
Why normal pivots cannot be asymmetric

For creating the pivot table above, I did 3 main steps:

  1. I put MonthInCalendar on columns
  2. I put the three measures for Total Due on values and
  3. I filtered MonthInCalendar, so that only Jan 2003 till March 2003 is still active for this pivot table

What does the pivot table do? It creates each combination of the three measures and the three Months from MonthInCalendar and puts it on columns. The result is a report with nine columns, but we only want those in colored squares, which represent the columns from the first screenshot. Or in other words: We want to define, which combination of MonthInCalendar and the choosen measures shall be created.

This is something you can’t do with normal pivots, but you can do this with pivots, that are built on Power Pivot data models. Let’s see how it works.

 

How you make your pivot asymmetric, working on a Power Pivot data model

There is a way to create asymmetric design, when your pivot is based on a Power Pivot data model (or any other OLAP data source, that can be queried by Multidimensional eXpressions (MDX)). You can build Named Sets to reach this target. Named Sets allow you to define the combination of values and dimension to be shown in the pivot table.

 

How to create Named Sets via wizard

Excel has a nice wizard, which let’s you create your Named Sets quite easy. The next figure shows you the 4 steps to reach the wizard:

  1. step: Click in the pivot table
  2. step: Click on the context sensitive menu bar PivotTable Tools
  3. step: Click on Fields, Items & Sets
  4. step: Click on Create Set Based on Column Items. It is also possible to create Named Sets based on rows, but this isn’t what we’re going to do here.
How to reach the Wizard for creating Named Sets
How to reach the Wizard for creating Named Sets

 

When you reached the wizard, you can create your first Named Set, which means you can create your first defined combination of column headers and values you want. To create your first Named Set follow these steps:

 

  1. Step: Give your set a name. If you are preparing a monthly report, I strongly suggest to give it a name like 200303, which stands for March 2003. In a later post I will show you how to control these sets via slicer and therefore this systematic is necessarry.
  2. Sets can be organized in folders. It’s not mendatory to do so, but I suggest, to organize your Sets in folders like MonthlyReport_TotalDue.
  3. Step: Uncheck the checkbox Replace the fields currently in the column are with the new set. If you don’t uncheck this box, Excel will throw out the current fields in the pivot and replace it by the newly created set. If you uncheck it here, the set will be created, but not automatically put into the pivot table. You will see later in this post, why this would leed to en error, if you wouldn’t uncheck this checkbox.
  4. & 5. Step: On the left side of the following figure you see the wizard, when it opens. You see all combinations of MonthInCalendar and Values, that exist in the current pivot table. First delete all the rows you don’t need (we only need 5 columns in the pivot table, so we only need 5 rows in this wizard) and then use the dropdowns, to select which MonthInCalendar and Values you need.
Create Named Sets via Wizard
Create Named Sets via Wizard

 

Finding the created Named Set(s) in the pivot table field list

When you are finished with the definition of the Named Set press OK. To find the Named Set in the pivot field list click into the pivot table (1), click on PivotTable Tools (2), click on tab Analyze (3) and Field List (4). Then you will find the newly created Named Set in the Field List, in folder MonthlyReports_TotalDue.

Find the Named Set in the pivot field list
Find the Named Set in the pivot field list
Now, that we have found our Named Sets, how can we use it in the pivot table?

How to use Named Sets in pivot tables

Knowing, where the Named Sets hide, is not enough. How can we use them in our pivot table. Left of the Named Set is a checkbox, which looks quite promissing. When you try to check that checkbox you get the following error message.
Using Named Sets in pivots_error message
Using Named Sets in pivots – error message
The error message says, that a Named Set can not be added to the pivot table, when the fields, which the Named Set bases on, are still in the pivot table. Read that sentence carefully. It is not only about the Values, but also about the Slicers and the columns. Before we can add the Named Set to the pivot table, we have to remove all measures from the Values and remove the slicer and columns aswell. When that is done, you can select the Named Set by clicking the checkbox. The result will look like in the following figure.
Selecting Named Sets in the PivotTable fields list
Selecting Named Sets in the PivotTable fields list
The pivot tables now has the shape we were looking for. The special thing with the Named Sets is, that you don’t see any of the fields in the Values field. You only see the Named Set on columns. This is because the Named Set defines the columns AND the values to choose.
You can define as many Named Sets as you like in the way you have seen in this post. Because there is no dropdown for existing Set folders, take care you write the folder names correct. Until now, we have the Named Sets with the structure we want. But when the month changes, you have to select the next Named Set for April 2003 in the field list.
How to choose Named Stes manually by PivotTable fields list
How to choose Named Sets manually by PivotTable fields list

 

This is not a big deal, but not that easy for someone who does not even know about Named Sets and their behavior.
In my next post How to control Named Sets via Slicer using MDX, I will show you, how you can control the selection of these Named Sets by slicer, what makes the user experience much nicer 🙂

See you next time and keep in mind: Sharing is caring. If you liked this articel, feel free to share it.

Greets from Hamburg,

Lars

Der Beitrag How to build asymmetric pivots on Power Pivot data models using Named Sets erschien zuerst auf THE SELF-SERVICE-BI BLOG.

]]>