Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Javier_Arze
New Member

Compute operations with values of subsamples of columns that respond dynamically to slicers

I have a dataset that has four columns: Country, Variable, Value, Year, and Vintage. The Country denotes country names of different 69 countries, Variables are 40 economic, Value is the actual numerical value for each variable, the year ranges from 2019-2028 and the Vintage is a text that  denotes a date in which a set of values was published (say “June 2023”, we two Vintages per year). So a short extract of the dataset in the first table below

 

The  objective is to set a page in which the user can select: 1) a Variable, and 2) two Vintages for which we need to compute the difference in the Values. For example, in the table above if the user selected the Variable: “GDP growth”, Vintage 1 = October 2020, and Vintage 2 = Oct 2021, the resulting column should list, for all the countries in the data, a column with the difference in the values of these two vintages for each year. 

 

This would be straightforward by using calculated columns and by reshaping the data so each Vintage is shown in a different column but we have too many vintages to create one for column for each and all the potential differences between the options. Hence, we would like the ability of a measure to responds dynamically to the filter context defined by the user’s selections of Variable and Vintages, but the result should still be a column not a single aggregate value.  I am not sure if this can be achieved in some way. 

 

I have attached below a small extract of the data in the first table, and one example of how the result would look like (in the thrird Table below) , given three selections of variable, and the two vintages a used may want to select using the slicers in third table (second table). Any advice woudl be greatly appreciated.    

 

CodeyearVariableValuevintage
1962019Debt Public debt32WEO_Oct2022
1962019Real GDP Growth3WEO_Oct2022
1962020Debt Public debt43WEO_Oct2022
1962020Real GDP Growth-2WEO_Oct2022
1962021Debt Public debt51WEO_Oct2022
1962021Real GDP Growth6WEO_Oct2022
1992019Debt Public debt56WEO_Oct2022
1992019Real GDP Growth0WEO_Oct2022
1992020Debt Public debt69WEO_Oct2022
1992020Real GDP Growth-6WEO_Oct2022
1992021Debt Public debt69WEO_Oct2022
1992021Real GDP Growth5WEO_Oct2022
2132019Debt Public debt89WEO_Oct2022
2132019Real GDP Growth-2WEO_Oct2022
2132020Debt Public debt103WEO_Oct2022
2132020Real GDP Growth-10WEO_Oct2022
2132021Debt Public debt81WEO_Oct2022
2132021Real GDP Growth10WEO_Oct2022
2182019Debt Public debt59WEO_Oct2022
2182019Real GDP Growth2WEO_Oct2022
2182020Debt Public debt78WEO_Oct2022
2182020Real GDP Growth-9WEO_Oct2022
2182021Debt Public debt80WEO_Oct2022
2182021Real GDP Growth6WEO_Oct2022
1962019Fiscal Rev.Tax.Income18WEO_Oct2022
1962020Fiscal Rev.Tax.Income19WEO_Oct2022
1962021Fiscal Rev.Tax.Income19WEO_Oct2022
1992019Fiscal Rev.Tax.Income14WEO_Oct2022
1992020Fiscal Rev.Tax.Income13WEO_Oct2022
1992021Fiscal Rev.Tax.Income14WEO_Oct2022
2132019Fiscal Rev.Tax.Income5WEO_Oct2022
2132020Fiscal Rev.Tax.Income5WEO_Oct2022
2132021Fiscal Rev.Tax.Income5WEO_Oct2022
2182019Fiscal Rev.Tax.IncomeWEO_Oct2022
2182020Fiscal Rev.Tax.IncomeWEO_Oct2022
2182021Fiscal Rev.Tax.IncomeWEO_Oct2022
1962019Debt Public debt32WEO_Oct2021
1962019Real GDP Growth2WEO_Oct2021
1962020Debt Public debt44WEO_Oct2021
1962020Real GDP Growth-2WEO_Oct2021
1962021Debt Public debt52WEO_Oct2021
1962021Real GDP Growth5WEO_Oct2021
1992019Debt Public debt56WEO_Oct2021
1992019Real GDP Growth0WEO_Oct2021
1992020Debt Public debt69WEO_Oct2021
1992020Real GDP Growth-6WEO_Oct2021
1992021Debt Public debt69WEO_Oct2021
1992021Real GDP Growth5WEO_Oct2021
2132019Debt Public debt89WEO_Oct2021
2132019Real GDP Growth-2WEO_Oct2021
2132020Debt Public debt103WEO_Oct2021
2132020Real GDP Growth-10WEO_Oct2021
2132021Debt Public debt WEO_Oct2021
2132021Real GDP Growth7WEO_Oct2021
2182019Debt Public debt59WEO_Oct2021
2182019Real GDP Growth2WEO_Oct2021
2182020Debt Public debt79WEO_Oct2021
2182020Real GDP Growth-9WEO_Oct2021
2182021Debt Public debt83WEO_Oct2021
2182021Real GDP Growth5WEO_Oct2021
1962019Fiscal Rev.Tax.Income18WEO_Oct2021
1962020Fiscal Rev.Tax.Income18WEO_Oct2021
1962021Fiscal Rev.Tax.Income18WEO_Oct2021
1992019Fiscal Rev.Tax.Income14WEO_Oct2021
1992020Fiscal Rev.Tax.Income13WEO_Oct2021
1992021Fiscal Rev.Tax.Income13WEO_Oct2021
2132019Fiscal Rev.Tax.Income5WEO_Oct2021
2132020Fiscal Rev.Tax.Income5WEO_Oct2021
2132021Fiscal Rev.Tax.Income5WEO_Oct2021
2182019Fiscal Rev.Tax.IncomeWEO_Oct2021
2182020Fiscal Rev.Tax.IncomeWEO_Oct2021
2182021Fiscal Rev.Tax.IncomeWEO_Oct2021
1962019Debt Public debt32WEO_Oct2020
1962019Real GDP Growth2WEO_Oct2020
1962020Debt Public debt48WEO_Oct2020
1962020Real GDP Growth-6WEO_Oct2020
1962021Debt Public debt60WEO_Oct2020
1962021Real GDP Growth4WEO_Oct2020
1992019Debt Public debt62WEO_Oct2020
1992019Real GDP Growth0WEO_Oct2020
1992020Debt Public debt79WEO_Oct2020
1992020Real GDP Growth-8WEO_Oct2020
1992021Debt Public debt83WEO_Oct2020
1992021Real GDP Growth3WEO_Oct2020
2132019Debt Public debt90WEO_Oct2020
2132019Real GDP Growth-2WEO_Oct2020
2132020Debt Public debt97WEO_Oct2020
2132020Real GDP Growth-12WEO_Oct2020
2132021Debt Public debt87WEO_Oct2020
2132021Real GDP Growth5WEO_Oct2020
2182019Debt Public debt59WEO_Oct2020
2182019Real GDP Growth2WEO_Oct2020
2182020Debt Public debt69WEO_Oct2020
2182020Real GDP Growth-8WEO_Oct2020
2182021Debt Public debt68WEO_Oct2020
2182021Real GDP Growth6WEO_Oct2020
1962019Fiscal Rev.Tax.Income18WEO_Oct2020
1962020Fiscal Rev.Tax.Income18WEO_Oct2020
1962021Fiscal Rev.Tax.Income17WEO_Oct2020
1992019Fiscal Rev.Tax.Income15WEO_Oct2020
1992020Fiscal Rev.Tax.Income13WEO_Oct2020
1992021Fiscal Rev.Tax.Income13WEO_Oct2020
2132019Fiscal Rev.Tax.Income5WEO_Oct2020
2132020Fiscal Rev.Tax.Income5WEO_Oct2020
2132021Fiscal Rev.Tax.Income5WEO_Oct2020
2182019Fiscal Rev.Tax.Income20WEO_Oct2020
2182020Fiscal Rev.Tax.Income22WEO_Oct2020
2182021Fiscal Rev.Tax.Income31WEO_Oct2020

 

Result if, for example:

User selection slicer 1: (linked to Variable)Debt Public debt
User selection slicer 2: (linked to Vintage) First VinstageWEO_Oct2020
User selection slicer 3: (linked to Vintage) Second VintageWEO_Oct2022

  

the reported value below are, for variable "Debt Public debt", = second vintage: (WEO_Oct 2022) [minus] value first vintage: (WEO_Oct 2020).

 

CodeyearVariableValue
1962019Debt Public debt0
1962020Debt Public debt-5
1962021Debt Public debt-9
1992019Debt Public debt-6
1992020Debt Public debt7
1992021Debt Public debt-14
2132019Debt Public debt-2
2132020Debt Public debt6
2132021Debt Public debt-6
2182019Debt Public debt0
2182020Debt Public debt9
2182021Debt Public debt12
1 ACCEPTED SOLUTION

@Sahir_Maharaj Thanks for your contribution on this thread.

Hi @Javier_Arze ,

@Sahir_Maharaj  have provided a possible solution. Here you can make a little adjustment to get your expected result:

1. Create two dimension tables(Do not create any relationship with your fact table)

Vintage1 = VALUES('ToolsPan_WEO'[vintage])
Vintage2 = VALUES('ToolsPan_WEO'[vintage])

2. Create two slicers which apply the field from the above 2 dimension tables

3. Create a measure as below

NValue = 
VAR _vintage1 =
    SELECTEDVALUE ( 'Vintage1'[vintage] )
VAR _vintage2 =
    SELECTEDVALUE ( 'Vintage2'[vintage] )
VAR _value1 =
    CALCULATE (
        SUM ( 'ToolsPan_WEO'[Value] ),
        FILTER ( 'ToolsPan_WEO', 'ToolsPan_WEO'[vintage] = _vintage1 )
    )
VAR _value2 =
    CALCULATE (
        SUM ( 'ToolsPan_WEO'[Value] ),
        FILTER ( 'ToolsPan_WEO', 'ToolsPan_WEO'[vintage] = _vintage2 )
    )
RETURN
    _value2 - _value1

vyiruanmsft_0-1711381547537.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Sahir_Maharaj
Super User
Super User

Hello @Javier_Arze,

 

Can you please try the following:

 

1. Ensure you have slicers set up for:

  • Variable (linked to your "Variable" column)
  • Vintage 1 (linked to your "Vintage" column)
  • Vintage 2 (linked to your "Vintage" column)

2. Create a Measure for Value Difference

Value Difference = 
VAR SelectedVariable = SELECTEDVALUE('YourTable'[Variable])
VAR Vintage1 = SELECTEDVALUE('YourTable'[Vintage], "Vintage1Default")
VAR Vintage2 = SELECTEDVALUE('YourTable'[Vintage], "Vintage2Default")
VAR ValueAtVintage1 = 
    CALCULATE(
        SUM('YourTable'[Value]),
        'YourTable'[Variable] = SelectedVariable,
        'YourTable'[Vintage] = Vintage1
    )
VAR ValueAtVintage2 = 
    CALCULATE(
        SUM('YourTable'[Value]),
        'YourTable'[Variable] = SelectedVariable,
        'YourTable'[Vintage] = Vintage2
    )
RETURN ValueAtVintage2 - ValueAtVintage1

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution?
➤ Lets connect on LinkedIn: Join my network of 13K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Thanks a lot Sahir_Maharaj.  I really apprecaite your help. I tried using the code you suggested but it did not work as desired. Initially I thought it could be realted to the fact that having two slicers linked to the same column: "vintage", may create a problem, so I copied the column vintage in the datasetm, and labeled the copy "vintage_2 ", then revised your code as follows:

Value Difference =
VAR SelectedVariable = SELECTEDVALUE('ToolsPan_WEO'[Variable])
VAR Vintage1 = SELECTEDVALUE('ToolsPan_WEO'[vintage], 0)
VAR Vintage2 = SELECTEDVALUE('ToolsPan_WEO'[vintage_2], 0)
VAR ValueAtVintage1 = CALCULATE(SUM('ToolsPan_WEO'[Value]),'ToolsPan_WEO'[Variable] = SelectedVariable, 'ToolsPan_WEO'[vintage] = Vintage1)
VAR ValueAtVintage2 = CALCULATE(SUM('ToolsPan_WEO'[Value]),'ToolsPan_WEO'[Variable] = SelectedVariable, 'ToolsPan_WEO'[vintage_2] = Vintage2)
RETURN
ValueAtVintage2-ValueAtVintage1
 
Yet this did not work eiter. I am reading about the use of Variables, on a DAX manual, and it states that in DAX, despite the name, any defined Variable is a constant. Once asigned a value the variable cannot be modified. Hence it seems to me that that is one of the reasons it would not work in this case, as the objective, is that the user can select one Variable and two Vintages from slicers in the page, and that the result table report the differences in value for the selected variable and two vintages. Other issue with the code you sugegsted is the SUM. There is nothing to be added in the dataset, because each row has a unique combination of counrty_code, variable, value, year, and vintage.  The result table should only pick two of these combinations for each country, reflecting two different vintages (selected by the user), and the variable selected, subtract those values, and return a table with the results for all the countries and years.     

@Sahir_Maharaj Thanks for your contribution on this thread.

Hi @Javier_Arze ,

@Sahir_Maharaj  have provided a possible solution. Here you can make a little adjustment to get your expected result:

1. Create two dimension tables(Do not create any relationship with your fact table)

Vintage1 = VALUES('ToolsPan_WEO'[vintage])
Vintage2 = VALUES('ToolsPan_WEO'[vintage])

2. Create two slicers which apply the field from the above 2 dimension tables

3. Create a measure as below

NValue = 
VAR _vintage1 =
    SELECTEDVALUE ( 'Vintage1'[vintage] )
VAR _vintage2 =
    SELECTEDVALUE ( 'Vintage2'[vintage] )
VAR _value1 =
    CALCULATE (
        SUM ( 'ToolsPan_WEO'[Value] ),
        FILTER ( 'ToolsPan_WEO', 'ToolsPan_WEO'[vintage] = _vintage1 )
    )
VAR _value2 =
    CALCULATE (
        SUM ( 'ToolsPan_WEO'[Value] ),
        FILTER ( 'ToolsPan_WEO', 'ToolsPan_WEO'[vintage] = _vintage2 )
    )
RETURN
    _value2 - _value1

vyiruanmsft_0-1711381547537.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.