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

MAX FUNCTION AND SLICER VALUE

My report contains a filter (titled ANNO) on which the user is able to select specific years from the data. The currenly selected value is 'Ultimi 2 anni' which means 'Last two years'. Applying this filter the dataset is filtered to 2019 and 2020 rows.

 

gabrielefugazzi_0-1605864142237.png

 

I've taken as a leading reference this customer record with selling amount per year extracting it directly from the tabular model I've created.

 

id                  2020   2019 totale

0201000084       0    8,52  8,52

 

It's clear that this specific customer has been inactive during 2020 and has performed some buying on 2019.

 

Since I wanted to buld two formulas to calculate the selling amount during last year and during previous year I've tried the following two formulas (fatturato_ac: selling_amount_last_year, fatturato_ap: selling_amount_previous_year).

 

Running the dax formula (where year is fixed) per last year and last year-1

 

fatturato_ac = VAR maxdate = 2020 return
if(isblank(round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2)), 0, round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2))
 
fatturato_ap = VAR maxdate = 2019 return
if(isblank(round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2)), 0, round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2))
 

produces the following matrix row

 

Cattura.JPG

 

which is the expected result.

 

But my desire was to use a parametric version of the formula where year is not fixed but directly linked to the selected values in the filter. So I developed the following formulas, keeping this calculation as the max year selector which I'm considering to be the max value in the filter.

 

VAR maxdate = max(fatturato[anno_fattura]) 

 

Running the dax formula (where year is not fixed) per last year and last year-1

 

fatturato_ac = VAR maxdate = max(fatturato[anno_fattura]) 
return
if(isblank(round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2)), 0, round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2))
 
fatturato_ap = VAR maxdate = max(fatturato[anno_fattura])-1
return
if(isblank(round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2)), 0, round(CALCULATE(sum(fatturato[fatturato]), fatturato[anno_fattura] = maxdate), 2))

 

Produce an undesired situation in which the max year is selected individually, row by row. Since my example customer has not performed any action this year the max year value is shifted on the previous year.

 

Cattura2.JPG

 

How can I use the formula correctly to get the results from the formula where the year was fixed by hand but with the use of a parameter?

 

Thank you in advance

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @gabrielefugazzi,

It seems like you are facing the find out the previous record in the discontinued table based on conditions, right?

If this is the case, I think you can extract the current date and calculate with a fixed numerical value should not suitable for our conditions.

You need to use condition and current date to filter on your table records to get a list of records, then extract the maximum one of them and this one should meet the previous records requirement.

 

Measure=
VAR currdate =
    MAX ( fatturato[anno_fattura] )
VAR prevdate =
    CALCULATE (
        MAX ( fatturato[anno_fattura] ),
        FILTER (
            ALL( fatturato ),
            [anno_fattura] < currdate
                && 'other conditions'
        )
    )
RETURN
    'your formula'

 

For measure on total level calculations, you can refer to the below blog:

Measure Totals, The Final Word 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

Hi @gabrielefugazzi,

It seems like you are facing the find out the previous record in the discontinued table based on conditions, right?

If this is the case, I think you can extract the current date and calculate with a fixed numerical value should not suitable for our conditions.

You need to use condition and current date to filter on your table records to get a list of records, then extract the maximum one of them and this one should meet the previous records requirement.

 

Measure=
VAR currdate =
    MAX ( fatturato[anno_fattura] )
VAR prevdate =
    CALCULATE (
        MAX ( fatturato[anno_fattura] ),
        FILTER (
            ALL( fatturato ),
            [anno_fattura] < currdate
                && 'other conditions'
        )
    )
RETURN
    'your formula'

 

For measure on total level calculations, you can refer to the below blog:

Measure Totals, The Final Word 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
amitchandak
Super User
Super User

@gabrielefugazzi , Not very clear to me. Have tried time intelligence with date table

example

YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))
This year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR('Date'[Date]),"12/31"))
Last year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))
Last to last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-2,Year),"12/31"))
Year behind Sales = CALCULATE(SUM(Sales[Sales Amount]),dateadd('Date'[Date],-1,Year))

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

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.