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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
StevenT
Resolver I
Resolver I

Get count of months data was larger or smaller than the previous month

Hi, is there a way to calculate how many times a record has values that are higher or lower than the previous month, as compared to going down the next month?

For example,
To calculate how many increases
in the first record the result would be 2, since only Feb and May show an increase
in the second record result would be 3, since Feb Apr and May show increases

To calculate how many decreases
in the first record the result would be 3, since only Jan March and Apr show an increase
in the second record result would be 3, since Jan and March show decreases

StevenT_1-1726251176363.png

 

 

2 ACCEPTED SOLUTIONS
v-jtian-msft
Community Support
Community Support

Hi,elitesmitpatel ,thanks for your concern about this issue.
And I would like to share some additional solutions below.

Hello,@StevenT.I am glad to help you.
According to your description, you want to calculate the number of times the measure [%diff] is greater than 0 or less than 0 (the number of times it has gone up or down compared to the previous month).
The count function does not work directly on the measure in Desktop, so I suggest that you first create a dummy table (using the summalize or filter function to specify the table) that meets the criteria for the measure of the drink.
Below is my test:
This is my test data: 

vjtianmsft_0-1726463656231.png
These are my test measures, in order to restore your real data as much as possible.

lastValue = 
VAR _date=MAX('Table'[Date])
VAR _lastValue=CALCULATE(MAX('Table'[C_Sales]),FILTER(ALLEXCEPT('Table','Table'[ID]),'Table'[Date]=EDATE(_date,-1)))
RETURN _lastValue

 

%Diff = 
VAR _result=IF(ISBLANK([lastValue]),0,
DIVIDE([lastValue]-MAX('Table'[C_Sales]),MAX('Table'[C_Sales]))
)
RETURN
_result

 

vjtianmsft_1-1726463756203.png

You can use the following code to calculate the result you want to achieve:

_countDrop = COUNTROWS(FILTER(ALLEXCEPT('Table','Table'[ID]),[%Diff]<0))



_countRise = COUNTROWS(FILTER(ALLEXCEPT('Table','Table'[ID]),[%Diff]>0))

vjtianmsft_2-1726463802519.png

It should be noted that the calculation results of measure is very dependent on your computing environment (filtering in the code and external visual and filters, slicer field filtering), so you need to modify the dax code according to your actual needs to calculate the mesaure to meet your actual data.I provide the test code for reference only.
I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.



View solution in original post

I'm going to try this out and definitely mark the Accept as Solution button.

View solution in original post

3 REPLIES 3
v-jtian-msft
Community Support
Community Support

Hi,elitesmitpatel ,thanks for your concern about this issue.
And I would like to share some additional solutions below.

Hello,@StevenT.I am glad to help you.
According to your description, you want to calculate the number of times the measure [%diff] is greater than 0 or less than 0 (the number of times it has gone up or down compared to the previous month).
The count function does not work directly on the measure in Desktop, so I suggest that you first create a dummy table (using the summalize or filter function to specify the table) that meets the criteria for the measure of the drink.
Below is my test:
This is my test data: 

vjtianmsft_0-1726463656231.png
These are my test measures, in order to restore your real data as much as possible.

lastValue = 
VAR _date=MAX('Table'[Date])
VAR _lastValue=CALCULATE(MAX('Table'[C_Sales]),FILTER(ALLEXCEPT('Table','Table'[ID]),'Table'[Date]=EDATE(_date,-1)))
RETURN _lastValue

 

%Diff = 
VAR _result=IF(ISBLANK([lastValue]),0,
DIVIDE([lastValue]-MAX('Table'[C_Sales]),MAX('Table'[C_Sales]))
)
RETURN
_result

 

vjtianmsft_1-1726463756203.png

You can use the following code to calculate the result you want to achieve:

_countDrop = COUNTROWS(FILTER(ALLEXCEPT('Table','Table'[ID]),[%Diff]<0))



_countRise = COUNTROWS(FILTER(ALLEXCEPT('Table','Table'[ID]),[%Diff]>0))

vjtianmsft_2-1726463802519.png

It should be noted that the calculation results of measure is very dependent on your computing environment (filtering in the code and external visual and filters, slicer field filtering), so you need to modify the dax code according to your actual needs to calculate the mesaure to meet your actual data.I provide the test code for reference only.
I hope my suggestions give you good ideas, if you have any more questions, please clarify in a follow-up reply.
Best Regards,
Carson Jian,
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.



I'm going to try this out and definitely mark the Accept as Solution button.

elitesmitpatel
Solution Supplier
Solution Supplier

Please attach the dummy PBIX file and required output.

Helpful resources

Announcements
September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Top Solution Authors