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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
sheorans352_1
New Member

Add calculated column by finding Difference between selected row and previous month data

sheorans352_1_0-1718972958638.png

 

 I need to find the difference of clicks to each query in a perticlar date 
In example : I wan to find difference betwen clicks for savings account ranking for URL 1 in May and Apr. I want the value in CLicks difference column against the Query 1. 

Should i use Unique IDs ? 


I ahve created a Unique Query ID  and i have tried using the following formula it gave error

ClicksDifference =
VAR CurrentQueryID = 'DS Page And Query Wise'[UniqueID]
VAR UnderscorePosition = FIND("_", CurrentQueryID, 1, LEN(CurrentQueryID)) // Find the first underscore position

// Extract current month substring after the last underscore
VAR CurrentMonth =
    MID(
        CurrentQueryID,
        UnderscorePosition + 1, // Start from the character after the underscore
        LEN(CurrentQueryID) - UnderscorePosition // Length of the substring
    )

// Assuming you have a mapping table for months like in the previous example
VAR MonthMapping =
    DATATABLE (
        "MonthName", STRING,
        "MonthNumber", INTEGER,
        {
            {"January", 1}, {"February", 2}, {"March", 3}, {"April", 4},
            {"May", 5}, {"June", 6}, {"July", 7}, {"August", 8},
            {"September", 9}, {"October", 10}, {"November", 11}, {"December", 12}
        }
    )

// Lookup current month number
VAR CurrentMonthNumber = LOOKUPVALUE(MonthMapping[MonthNumber], MonthMapping[MonthName], CurrentMonth)

// Calculate previous month number
VAR PreviousMonthNumber = IF(CurrentMonthNumber = 1, 12, CurrentMonthNumber - 1)

// Lookup previous month name
VAR PreviousMonthName = LOOKUPVALUE(MonthMapping[MonthName], MonthMapping[MonthNumber], PreviousMonthNumber)

// Replace current month with previous month in QueryID
VAR PreviousQueryID = SUBSTITUTE(CurrentQueryID, "_" & CurrentMonth, "_" & PreviousMonthName)

// Calculate previous month clicks
VAR PreviousClicks =
    CALCULATE(
        SUM('DS Page And Query Wise'[Url Clicks]),
        FILTER(
            'DS Page And Query Wise',
            'DS Page And Query Wise'[UniqueID] = PreviousQueryID
        )
    )

// Calculate current month clicks
VAR CurrentClicks = SUM('DS Page And Query Wise'[Url Clicks])

// Calculate clicks difference
RETURN
    IF(
        ISBLANK(PreviousClicks),
        BLANK(),
        CurrentClicks - PreviousClicks
    )
 
 
Second Query I used 
Clicks Difference =
VAR SelectedClicks = 'DS Page And Query Wise'[Url Clicks]
VAR SelectedLOB = 'DS Page And Query Wise'[LOB ]
VAR SelectedQuery = 'DS Page And Query Wise'[Query]
VAR SelectedLandingPage = 'DS Page And Query Wise'[Landing Page]
VAR SelectedDate = 'DS Page And Query Wise'[Date (Year Month)]

VAR PriorMonth = EOMONTH(SelectedDate, -1)

VAR PreviousMonthClicks =
    CALCULATE(
        SUM('DS Page And Query Wise'[Url Clicks]),
        FILTER(
            'DS Page And Query Wise',
            'DS Page And Query Wise'[Date (Year Month)] = PriorMonth &&
            'DS Page And Query Wise'[LOB ] = SelectedLOB &&
            'DS Page And Query Wise'[Query] = SelectedQuery &&
            'DS Page And Query Wise'[Landing Page] = SelectedLandingPage
        )
    )

RETURN
    IF( 'DS Page And Query Wise'[LOB ] = SelectedLOB &&
            'DS Page And Query Wise'[Query] = SelectedQuery &&
            'DS Page And Query Wise'[Landing Page] = SelectedLandingPage &&
            'DS Page And Query Wise'[Date (Year Month)] = PriorMonth,
            SelectedClicks - PreviousMonthClicks,
            BLANK()
        )
2 REPLIES 2
Anonymous
Not applicable

Hi @sheorans352_1 

 

Could you please provide more raw data in your tables (exclude sensitive data) with Text format and your expected result with backend logic and special examples? It would be helpful to find out the solution. You can refer the following links to share the required info:

How to provide sample data in the Power BI Forum

How to Get Your Question Answered Quickly

And It is better if you can share a simplified pbix file. You can refer the following link to upload the file to the community. Thank you.

How to upload PBI in Community

 

Best Regards

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

 

Updater the dummy data with calcualtion and their reason? Can you help now ? 

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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