The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Hi
I’m looking for helping to write a DAX measure that selects dynamic columns based on slicer selection.
I have a large dataset on power query that has hundreds of columns. I’m creating a dashboard where one of the slicers will be ‘dates’ allowing user to enter a ‘from’ (start date) and ‘to’ (end date).
Once user enters the dates, the DAX measure should do the following:
For visualisation, this is how the main data looks like:
Portfolio | 31/07/2020 | 01/08/2020 | 02/08/2020 | 03/08/2020 | 04/08/2020 | 05/08/2020 |
P1 | 100.0 | 100.5 | 101.0 | 101.5 | 102.0 | 102.5 |
P2 | 100.0 | 101.0 | 102.0 | 103.0 | 104.0 | 105.0 |
P3 | 100.0 | 105.0 | 110.0 | 115.0 | 120.0 | 125.0 |
If a user selects start date as 02/08/2020 and end date as 04/08/2020, the expected results is as tabled below. The first column (‘Portfolio’) will be static whilst the columns with header dates will be dynamic based on user selections.
Portfolio | 02/08/2020 | 04/08/2020 | Return |
P1 | 101.0 | 102.0 | 1.0 |
P2 | 102.0 | 104.0 | 2.0 |
P3 | 110.0 | 120.0 | 9.0 |
I would really appreciate your help in writing a measure that can do the above.
Thanks
B
Hi @Bobstar86 ,
I suggest you unpivot your table by select all other columns except [Portfolio] column.
New table should look like as below.
Measure:
Measure =
VAR _StartDate = SELECTEDVALUE('Start Date'[Start Date])
VAR _EndDate = SELECTEDVALUE('End Date'[End Date])
VAR _Value = CALCULATE(SUM('Table'[Value]),FILTER('Table','Table'[Date] = _StartDate ||'Table'[Date] = _EndDate))
VAR _StartValue = CALCULATE(SUM('Table'[Value]),FILTER('Table','Table'[Date] = _StartDate))
VAR _EndValue = CALCULATE(SUM('Table'[Value]),FILTER('Table','Table'[Date] = _EndDate))
RETURN
IF(ISFILTERED('Start Date'[Start Date])||ISFILTERED('End Date'[End Date]),IF(HASONEVALUE('Table'[Date]),_Value,_EndValue - _StartValue),IF(HASONEVALUE('Table'[Date]),SUM('Table'[Value])))
Create a matrix visual and rename column subtotal as Return. Create two unrelated date tables to create start date slicer and end date slicer.
Result is as below.
By Default:
Startdate =2020/08/02, Enddate =2020/08/04
Best Regards,
Rico Zhou
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @Anonymous
This is very helpful and is exactly what I was after. Thanks very much.
If you don't mind, I have a few follow up questions that I would appreciate your help with:
Once again thank you so much
Bobstar86
User | Count |
---|---|
17 | |
8 | |
7 | |
6 | |
6 |
User | Count |
---|---|
26 | |
13 | |
12 | |
9 | |
8 |