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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
thampton
Helper III
Helper III

Dynamic Column based on slicer

I have a table with a sales date (per order) and then a disconnected date table. I would like to be able to calculate a dynamic aging based on the slicer selection. 

 

For example, sales order date is 6/07/2019, if i select 6/11/2019, the column would calculate 4. If i select 6/09/2019, the column would calculate 2. 

 

Is there any way to do this? 

1 ACCEPTED SOLUTION
Cmcmahan
Resident Rockstar
Resident Rockstar

Does this answer look close to what you want? https://community.powerbi.com/t5/Desktop/Dynamic-Column-Calculation-Based-on-Multiple-Slicers/td-p/1...

 

If so, you can use their answer, but instead of summing the values, you should be able to use something like this:

MONTHDIFF =  CALCULATE (DATEDIFF(Table1[OrderDate], EARLIEST(Table2[SelectedDate]), month ), ALLSELECTED () )

Set your slicer to select values from your independant date table, and you should be good to go.

View solution in original post

6 REPLIES 6
Cmcmahan
Resident Rockstar
Resident Rockstar

Does this answer look close to what you want? https://community.powerbi.com/t5/Desktop/Dynamic-Column-Calculation-Based-on-Multiple-Slicers/td-p/1...

 

If so, you can use their answer, but instead of summing the values, you should be able to use something like this:

MONTHDIFF =  CALCULATE (DATEDIFF(Table1[OrderDate], EARLIEST(Table2[SelectedDate]), month ), ALLSELECTED () )

Set your slicer to select values from your independant date table, and you should be good to go.

@Cmcmahan Thanks for the reply! For some reason, it is not allowing me to use column in the datediff function; It doesnt pull up the column list. Any ideas?

What happens when you just type in the column manually?  Do you get an error message?

I've found that sometimes the autocomplete doesn't fill in, but you can still accomplish the task.

Ah, I figured out the issue after trying it myself.  The issue is that Power BI doesn't know which row you're referring to when creating the measure, so it assumes it will get multiple results.  The easiest way to fix this is to use one of the aggregation functions to turn a column into one value. 

Here I used SELECTEDVALUE, which will return whatever the value is if there's only one option, otherwise it will return a blank by default.    

 

MONTHDIFF =  CALCULATE (DATEDIFF(SELECTEDVALUE(Table1[OrderDate]), EARLIEST(Table2[SelectedDate]), month ), ALLSELECTED () )

 When you're only using one date (like in a table/matrix) the filtering is already done for you, so there is only one value to select from.  If you're doing something else with your data, you can use an AVERAGE, EARLIEST, or some other aggregation function as fits the scenario.

@Cmcmahan 

 

The goal for this would be to select a date in the slicer, have the column calculate the aging, then distribute these into buckets (i can do the bucket in a IF statement). 

 

A problem im runnning into: the SELECTEDVALUE worked for the table column but when i try to select the unrelated date table, it throws the error below. This is the table that is connected to my slicer. 

 

"EARLIER/EARLIEST refers to an earlier row context which doesn't exist."

Try SELECTEDVALUE instead of EARLIEST in your independent table.

 

You may want to set a default for SELECTEDVALUE so that when you have multiple/no selection you don't get errors.

I would first try 

SELECTEDVALUE(MONTH(DateTable[Date]))

And if that gives you weirdness, try

SELECTEDVALUE(MONTH(DateTable[Date]), <default value>)

replacing <default value> with a default date of some sort

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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