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
PietroFarias
Resolver II
Resolver II

Change in the value of the current period with the last non-blank period

Some time ago, I had to return the variation from the sale of the current day to the last sale made, in which it would not necessarily be the previous day always or before the previous day. I need help now.

 

 

SellersCustomerDateSale
BethWWW22/04/2017 $ 3,97
BethWWW07/05/2017 $ 3,12
BethWWW07/01/2018 $ 1,41
PaulPKZ14/01/2017 $ 3,25
PaulPKZ02/04/2017 $ 2,97
PaulPKZ16/02/2018 $ 3,47
PeterXYZ08/01/2018 $ 3,45
PeterXYZ10/03/2018 $ 3,81
PeterXYZ22/03/2018 $ 4,00
PeterXYZ23/05/2018 $ 1,29

 

 

I need to return the variation from the average sale of the month to the last month. Peter, for example, made a sale in May. And before it did two sales in March. How do I return the variation of these two periods in the DAX?

1 ACCEPTED SOLUTION

Thanks Sheng.

 

But, your formula returned ever the previous month and not the last period in which the sale took place, sometimes it was many months ago.

So, i did that:

I created a Calculated Column with LastPeriod

 

= VAR LstPeriod =
MAXX (
    FILTER (
            Sales;
        Sales[Date] <= EOMONTH(EARLIER(Sales[Date]);-1)
            && Sales[Saller] = EARLIER ( [Saller] )
            && Sales[Customer] = EARLIER( [Customer])
    );
    Sales[Date]
)
RETURN
    FORMAT(YEAR(LstPeriod);"0000") & "-" & FORMAT(MONTH(LstPeriod);"00")

 

And another calculated column with the AVERAGE of this last period.

 

 

=CALCULATE(
		Sales[LastPeriod];
		FILTER(
			Sales;
			[CurrentPeriod] = EARLIER([LastPeriod])
				&& [Customer] = EARLIER([Customer])
				&& [Saller] = EARLIER([Saller])
		)
)

 

And ready. Create a measure with variation.

MedRejuste:=DIVIDE(
	AVERAGE([Sale]) - AVERAGE(AvgLastPeriod);
	AVERAGE(AvgLastPeriod);
	BLANK()
)

 

If you see something that can improve, it will be helpful.

View solution in original post

3 REPLIES 3
v-shex-msft
Community Support
Community Support

HI @PietroFarias,


>>How do I return the variation of these two periods in the DAX?

You can try to use below measures to get average values from different period.

Avg of Previous Period= 
VAR currDate =
    MAX ( Table5[Date] )
RETURN
    CALCULATE (
        AVERAGE ( Table5[Sale] ),
        FILTER (
            ALLSELECTED ( Table5 ),
            FORMAT ( [Date], "mm/yyyy" )
                = FORMAT ( DATE ( YEAR ( currDate ), MONTH ( currDate ) - 1, 1 ), "mm/yyyy" )
        ),
        VALUES ( Table5[Sellers] ),
        VALUES ( Table5[Customer] )
    )

Avg of Current Period= 
VAR currDate =
    MAX ( Table5[Date] )
RETURN
    CALCULATE (
        AVERAGE ( Table5[Sale] ),
        FILTER (
            ALLSELECTED ( Table5 ),
            FORMAT ( [Date], "mm/yyyy" ) = FORMAT ( currDate, "mm/yyyy" )
        ),
        VALUES ( Table5[Sellers] ),
        VALUES ( Table5[Customer] )
    )

 

Regards,

Xiaoxin Sheng

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

Thanks Sheng.

 

But, your formula returned ever the previous month and not the last period in which the sale took place, sometimes it was many months ago.

So, i did that:

I created a Calculated Column with LastPeriod

 

= VAR LstPeriod =
MAXX (
    FILTER (
            Sales;
        Sales[Date] <= EOMONTH(EARLIER(Sales[Date]);-1)
            && Sales[Saller] = EARLIER ( [Saller] )
            && Sales[Customer] = EARLIER( [Customer])
    );
    Sales[Date]
)
RETURN
    FORMAT(YEAR(LstPeriod);"0000") & "-" & FORMAT(MONTH(LstPeriod);"00")

 

And another calculated column with the AVERAGE of this last period.

 

 

=CALCULATE(
		Sales[LastPeriod];
		FILTER(
			Sales;
			[CurrentPeriod] = EARLIER([LastPeriod])
				&& [Customer] = EARLIER([Customer])
				&& [Saller] = EARLIER([Saller])
		)
)

 

And ready. Create a measure with variation.

MedRejuste:=DIVIDE(
	AVERAGE([Sale]) - AVERAGE(AvgLastPeriod);
	AVERAGE(AvgLastPeriod);
	BLANK()
)

 

If you see something that can improve, it will be helpful.

PietroFarias
Resolver II
Resolver II

Somebody? Smiley Sad

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.