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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Bobstar86
New Member

Selecting dynamic colums based on slicer selection

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:

  1. Filter the main data set and produce a subset data table based on the user’s selection.  
  2. Add a new column that calculates percentage return between these two dates. The percentage return will be ‘Return = Divide ((end date, start date – 1*100)’.  

For visualisation, this is how the main data looks like:

Portfolio31/07/202001/08/202002/08/202003/08/202004/08/202005/08/2020
P1100.0100.5101.0101.5102.0102.5

P2

100.0101.0102.0103.0104.0105.0
P3100.0105.0110.0115.0120.0125.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.

Portfolio02/08/202004/08/2020Return
P1101.0102.01.0
P2102.0104.02.0
P3110.0120.09.0

I would really appreciate your help in writing a measure that can do the above.

 

Thanks

 

B

 

2 REPLIES 2
Anonymous
Not applicable

Hi @Bobstar86 ,

 

I suggest you unpivot your table by select all other columns except [Portfolio] column.

New table should look like as below.

RicoZhou_0-1653533589680.png

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.

RicoZhou_1-1653533661521.png

Result is as below.

By Default:

RicoZhou_2-1653533671838.png

Startdate =2020/08/02, Enddate =2020/08/04

RicoZhou_3-1653533749258.png

 

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:

  1. Do I therefore need to set the data source as an unpivoted table?
  2. Is there a way of preventing user from selecting multiple dates?
  3. Is there a way to set the date slicers such that a user cannot input a ‘To’ date earlier than 'From' date?
  4. My dataset has hundrends of date columns (in pivoted format). Setting the date slicer as drop down means there will be alot of scrolling up and down to select a date. Setting the slicer as between dates (similar to below) with a calendar is much more efficient. Is there a way of doing this instead of the drop down.

Picture1.png

Once again thank you so much

 

Bobstar86

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.