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
Mario1000
Advocate I
Advocate I

Dynamic flag with a date slicer

Hi community,


Is it possible to create a dynamic column or something with the same functionality? I need this to flag the rows in a table dinamically depending on a date slicer. 

 

So I have a table with 3 columns: Store, Date and value. Here a simplified example:

StoreDateValue
A01/01/20191
A03/03/20192
A05/05/20193
B02/02/20194
B04/04/20195
B08/08/20196

 

For all my calculations I need to use only the values in the last date for each store. So I first created a column that flags the last date for each row:

 

StoreDateValueFlag
A01/01/201910
A03/03/201920
A05/05/201931
B02/02/201940
B04/04/201950
B08/08/201961

 

The problem here is that this is an static calculation and what I need is this flag to be referenced to a date slicer in the dashboard, so that if the slicer is set to 06/06/2019 for example, the flag would look like this:

 

StoreDateValueFlag
A01/01/201910
A03/03/201920
A05/05/201931
B02/02/201940
B04/04/201951
B08/08/201960

 

I need the flag to then make some calculations like these:

KPI =
CALCULATE(
        SUM(Value)
        Filter(Table; Flag=1))

The way the flag is made right now, it considers only the last visit for each store for the current date, but when I go back in time with the slicer it filters the data instead of recalculating the flag.

 

Is there any way to get this?

I already tried building a table with groupby so that I have the last date for each store but again the same problem, the table is static and doesn't update with the slicer

 

NOTE = The real data set contains thousands of stores and dates, so any "manual" solution is not useful.

 

Thank you in advance for your time and help!

1 ACCEPTED SOLUTION

Hi @Mario1000 

 

Try something like this.

 

filter = 
VAR __maxSelectedDate = 
    GROUPBY(
        CALCULATETABLE(
            FILTER( 
                'Table',
                'Table'[Date] <= MAX( 'Dates'[Date] ) 
            ),
            ALLEXCEPT( 'Table', 'Table'[Store] ) ),
        'Table'[Store],
        "@maxDate", MAXX( CURRENTGROUP(), 'Table'[Date] )
    ) 
RETURN 
    CALCULATE( 
        COUNTROWS( 'Table' ),
        KEEPFILTERS( 
            TREATAS( __maxSelectedDate, 'Table'[Store], 'Table'[Date] )
        )
    )

 

Sum of filter = 
CALCULATE( 
    SUM( 'Table'[Value] ), 
    FILTER( 'Table' , [filter] ) 
)
Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

 

View solution in original post

10 REPLIES 10
Anonymous
Not applicable

Woow, that is excelent solution! :-).

One question thought, is there a way to use it with the line chart visual?

Mariusz
Community Champion
Community Champion

Hi @Mario1000 

 

You can create a measure like below.

Flag = 
VAR __selectedDate = MAX( Dates[Date] )
RETURN 
INT( 
    MAX( 'Table'[Date] ) = 
    CALCULATE(  
        MAX( 'Table'[Date] ),
        'Table'[Date] <= __selectedDate 
    )
)

image.png

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

 

Thanks @Mariusz !

 

This measure gets the desired result. However I need the flag to use it as a filter in further calculations. Imagine a simple one, the sum of 'Value' (for the last date on each store). If the flag was a column we would do something like this:

CALCULATE(
     SUM(Table[Value]);
     FILTER(Table; Table[Flag]=1))

How can I filter in this case as this flag is a measure?

Thanks!

 

 

Hi @Mario1000 ,

 

You can directly create a quick measure: 

Hi @saraMissBI ,

 

What I need is to flag a condition to filter in further KPI calculations. This condition as said before is that I only want to take into consideration the last date for each store.

 

@Mariusz proposed this measure ( I just added the last condition for other purposes):

 

 

 

Flag_ = 
VAR __selectedDate = MAX('Calendar'[Date])
VAR Calculo=
INT( 
    MAX(Facts[Date]) = 
    CALCULATE(  
        MAX(Facts[Date]);
        Facts[Date] <= __selectedDate 
    )
)

RETURN
IF(ISBLANK(SUM(Facts[Value]));BLANK();Calculo)

 

 

 

Now this allows me to flag the rows in a table visual, but it's not the final purposes as said in the beginning. I create a KPI which is the sum of values for each store on the last date according to slicer, using the previous flag:

 

 

KPI = 
VAR Flag = [Flag_]

RETURN 
CALCULATE(
   SUM(Facts[Value]);
   FILTER('Facts';Flag=1))

 

 

 

But the results are not correct when visualized on bar chart or cards:

 

qwe.jpg

So  the question is, how do I use the flag to filter on a KPI measure? Or is there any other workaround?


Thanks!

 

Hi @Mario1000 

 

Try something like this.

 

filter = 
VAR __maxSelectedDate = 
    GROUPBY(
        CALCULATETABLE(
            FILTER( 
                'Table',
                'Table'[Date] <= MAX( 'Dates'[Date] ) 
            ),
            ALLEXCEPT( 'Table', 'Table'[Store] ) ),
        'Table'[Store],
        "@maxDate", MAXX( CURRENTGROUP(), 'Table'[Date] )
    ) 
RETURN 
    CALCULATE( 
        COUNTROWS( 'Table' ),
        KEEPFILTERS( 
            TREATAS( __maxSelectedDate, 'Table'[Store], 'Table'[Date] )
        )
    )

 

Sum of filter = 
CALCULATE( 
    SUM( 'Table'[Value] ), 
    FILTER( 'Table' , [filter] ) 
)
Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

 

Mariusz
Community Champion
Community Champion

Hi @Mario1000 

 

Also, you can use the filter measure as a visual filter like below, then you don't need to add it to every measure.

image.png

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

 

Mariusz
Community Champion
Community Champion

Hi @Mario1000 

 

Please see the attached file with the solution included.

 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

 

Thanks @Mariusz that solved my problem indeed!

 

Hi @Mario1000 ,

I am sorry my reply was incomplete; I lost some snapshots I included..(something must have gone wrong when posting)

I understand what you need. Could you please have a look at the following snapshots and give me your feedback if that helps to serve what you need?

 

snap1.pngsnap2.pngsnap4.png

You can see that you automatically get the sum of "Value" for the stores corresponding to latest dates for each store given the selection in the slicer. I think this solution is a shortcut, please let me know what you think.

 

Best regards,

 

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.

Top Solution Authors