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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

How to calculate relative change in comparison to prior month

Hello,

I have a table called "Searchterms_View" which contains all search queries that were entered on our website. 

The table contains a row for each search, for simplicity reasons lets say it only contains two columns the sessionId which is a unique identifier for every session and the searchQuery which contains the term the user typed.

 

Through the sessionId I can match each search to Date which lies in Dimension_View. 

I now want to determine how large the share of each searchterm is in the last 30 days and how that changed in relation to the prior 30 days.

 

So let's say 4/20 searches in the last 30 days were "battery" then that would amount to 20% in that month. But maybe the 30 days prior that figure was (15/100) 15%, then I would want the outcome to be +33%.

 

So far I have gotten this:

VAR last_30 =
CALCULATE(COUNT(Searchterms_View[searchQuery]), DATESINPERIOD(Dimension_View[date], LASTDATE(Dimension_View[date]), -30, DAY))
VAR prior_30 = CALCULATE(COUNT(Searchterms_View[searchQuery]), DATESINPERIOD(Dimension_View[date], LASTDATE(Dimension_View[date])-30, -60, DAY))
RETURN last_30/prior_30 - 1
 
This gets me how the absolute number has evolved, so the drop from 15 to only 4 would leave me with -73%, because the absolute number has decreased, meanwhile the relative share has increased. How would I do this? 
 
Cheers.
1 ACCEPTED SOLUTION

Hi @Anonymous ,

 

Maybe you can try this code:

Measure = 
VAR _maxdate =
    CALCULATE( MAX( 'Dimension_View'[Date] ), REMOVEFILTERS(Searchterms_View[searchQuery] ))
VAR _last_30d = _maxdate - 30
VAR _last_60d = _last_30d - 30
VAR _last_30 =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _maxdate,
        'Dimension_View'[Date] > _last_30d
    )
VAR _last_30_total =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _maxdate,
        'Dimension_View'[Date] > _last_30d,
        REMOVEFILTERS( Searchterms_View[searchQuery] )
    )
VAR _last_60 =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _last_30d,
        'Dimension_View'[Date] > _last_60d
    )
VAR _last_60_total =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _last_30d,
        'Dimension_View'[Date] > _last_60d,
        REMOVEFILTERS( Searchterms_View[searchQuery] )
    )
RETURN
    DIVIDE(
        DIVIDE( _last_30, _last_30_total ),
        DIVIDE( _last_60, _last_60_total )
    )-1

Result:

vchenwuzmsft_0-1642145346568.png

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

4 REPLIES 4
amitchandak
Super User
Super User

@Anonymous , I think this should be 30 only

 

VAR prior_30 = CALCULATE(COUNT(Searchterms_View[searchQuery]), DATESINPERIOD(Dimension_View[date], LASTDATE(Dimension_View[date])-30, -30, DAY))

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

You are right, that is not the core issue though. Check out the reply below, I generated some sample data which might help. 🙂 

 

This is what my current measure is getting for me, relative change between the absolute value, however I want the relative change between the share a searchterm made up in that month.

This is what I am currently getting, it displays the relative change between the two absolute values. However I want the relative change between the relative share.This is what I am currently getting, it displays the relative change between the two absolute values. However I want the relative change between the relative share.

 

The data is pasted seems to have been deleted, so I put it into a pastebin:

Searchterms_View:

https://pastebin.com/f94xknUf

Dimension_View:

https://pastebin.com/SCnVHc9g

Hi @Anonymous ,

 

Maybe you can try this code:

Measure = 
VAR _maxdate =
    CALCULATE( MAX( 'Dimension_View'[Date] ), REMOVEFILTERS(Searchterms_View[searchQuery] ))
VAR _last_30d = _maxdate - 30
VAR _last_60d = _last_30d - 30
VAR _last_30 =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _maxdate,
        'Dimension_View'[Date] > _last_30d
    )
VAR _last_30_total =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _maxdate,
        'Dimension_View'[Date] > _last_30d,
        REMOVEFILTERS( Searchterms_View[searchQuery] )
    )
VAR _last_60 =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _last_30d,
        'Dimension_View'[Date] > _last_60d
    )
VAR _last_60_total =
    CALCULATE(
        COUNT( Searchterms_View[searchQuery] ),
        'Dimension_View'[Date] <= _last_30d,
        'Dimension_View'[Date] > _last_60d,
        REMOVEFILTERS( Searchterms_View[searchQuery] )
    )
RETURN
    DIVIDE(
        DIVIDE( _last_30, _last_30_total ),
        DIVIDE( _last_60, _last_60_total )
    )-1

Result:

vchenwuzmsft_0-1642145346568.png

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Sir, I don't think you have any idea, how much I love you right now. If I could kiss you I would.

I have probably wasted 5-10 hours on this and we even had a Power BI consultant here who pointed me in a completely different direction and you have this very elegant solution without need for any new columns or even tables.


I am very, very grateful. Thank you so much.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors