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! Request now

Reply

A measure or a column that subtract a specific row with conditions from current row

I would like to create a new measure or a new column that calculates the difference of current row to a specific row. So the logic is that each row to subtract the row with same company, company name and same year Q1. 
if the row is "co001" && "companyA" && "co001&companyA" && [Quarter] = "2021 Q2" then return 90-100, where 100 is the same company in "2021 Q1"
if the row is  "co001" && "companyA" && "co001&companyA" && [Quarter] = "2021 Q1" then pass to next row for new calculation
if current row is "2022 Q2" then "2022 Q1" of the same company does not exist, return null


Below tables are examples of what I working on, [sale_difference] is the column output I desire. 

Table1:

Companycompany nameQuarterco&co_namesale_amtsale_difference
co001companyA1co001&companyA100null
co002companyJ1co002&companyJ98null
co003companyQ2co003&companyQ971
co004companyH2co004&companyH100 
co001companyA2co001&companyA90-10
co001companyA5co001&companyA95null
co002companyJ1co002&companyJ9292
co004companyH5co004&companyH99null
co004companyH6co004&companyH89-10
co003companyQ1co003&companyQ9696

  

 

Table2:

QuarterIDQuarter_Name
12021 Q1
22021 Q2
32021 Q3
42021 Q4
52022 Q1
62022 Q2
72022 Q3
8

2022 Q4

 

 

--get table2 quarter name based on quarter id in table1
VAR _targetQ = 
LOOKUPVALUE('Table2'[quarter_name], 
            'Table2'[quarterID], 
            'Table1'[Quarter])

--get quarter name as in "yyyy Q1"
VAR _targetQCri = 
CONCATENATE (LEFT(_targetQ , 4), " ", "Q1")

--get quarter ID for table1 lookup
VAR _targetQID = 
LOOKUPVALUE('Table2'[QuarterID], 
            'Table2'[quarter_name], 
            _targetQCri)

--get current row's year Q1 sale amount
VAR _targetQSale = 
LOOKUPVALUE('Table1'[sale_amt], 
            'Table1'[Company], 
            'Table1'[Company],
            'Table1'[company name], 
            'Table1'[company name], 
            'Table1'[co&co_name], 
            'Table1'[co&co_name], 
            'Table1'[Quarter],
            _targetQID
)
 
RETURN 
/*if current row quarterID contains "Q1", go to next row, else the current row minus the same year Q1*/
IF ( CONTAINSSTRING(_targetQ , "Q1") , pass, 
     CALCULATE(
     'Table1'[sale_amt] - _targetQSale
     )

 

 

I have above code, but it seems not working and I dont know how to get this in a loop for each row to perform the calculation in a measure. Thank you in advance. 

1 ACCEPTED SOLUTION

It seems like the code is only good for one quarter before the current quarter. I should've include q3 and q4 so that 2022 q3 can subtract 2022 q1 as an example. I developed below code but it's only good for column setup. Is it possible to make it a measure? why measure does not do lookupvalue like column?

 

sale_exclude_q1 = 
VAR _corrQ1 = 
LOOKUPVALUE(Table1[sale_amt], 
    Table1[co&co_name], 
    Table1[co&co_name],
    Table1[Quarter], 
    LOOKUPVALUE(
        Table2[QuarterID], Table2[Quarter_Name], 
        CONCATENATE(
            LEFT(
                LOOKUPVALUE(Table2[Quarter_Name], Table2[QuarterID], Table1[Quarter])
                ,4
            ), " Q1"
        )
    )
)

RETURN 
IF(OR(CONTAINSSTRING(LOOKUPVALUE(Table2[Quarter_Name], Table2[QuarterID], Table1[Quarter]), " Q1"), _corrQ1 = BLANK()), 
    BLANK(),
    Table1[sale_amt] - _corrQ1
)

 

View solution in original post

6 REPLIES 6
parry2k
Super User
Super User

@learninPBI5Hard use TI functions:  

As a best practice, add a date dimension in your model and use it for time intelligence calculations. Once the date dimension is added, mark it as a date table on table tools. Check the related videos on my YT channel

 

Add Date Dimension
Importance of Date Dimension
Time Intelligence Playlist

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Im sorry for the confusion. I understand that q1 can be translated as other value type like time or number, but my actual data cannot be translated to time. The data is more like "2022 normal", "2022 interestRate down", or "2022 int volitility up". So the logic is like different scenario impacts from 'normal' of the year.  

parry2k
Super User
Super User

@learninPBI5Hard solution is attached, tweak it as you see fit. The best method though is to convert Quarter to end of the quarter date and then use time intelligence functions, although the attached solution is based on your sample data (not using TI functions)



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

I have a sample file, I cannot find the upload portal for that. is there any other way to share my file? 

It seems like the code is only good for one quarter before the current quarter. I should've include q3 and q4 so that 2022 q3 can subtract 2022 q1 as an example. I developed below code but it's only good for column setup. Is it possible to make it a measure? why measure does not do lookupvalue like column?

 

sale_exclude_q1 = 
VAR _corrQ1 = 
LOOKUPVALUE(Table1[sale_amt], 
    Table1[co&co_name], 
    Table1[co&co_name],
    Table1[Quarter], 
    LOOKUPVALUE(
        Table2[QuarterID], Table2[Quarter_Name], 
        CONCATENATE(
            LEFT(
                LOOKUPVALUE(Table2[Quarter_Name], Table2[QuarterID], Table1[Quarter])
                ,4
            ), " Q1"
        )
    )
)

RETURN 
IF(OR(CONTAINSSTRING(LOOKUPVALUE(Table2[Quarter_Name], Table2[QuarterID], Table1[Quarter]), " Q1"), _corrQ1 = BLANK()), 
    BLANK(),
    Table1[sale_amt] - _corrQ1
)

 

foodd
Super User
Super User

Please create a pbix file that contains some sample data but still reflects your data model (tables, relationships, calculated columns, and measures), upload the pbix to Onedrive or Dropbox, and share the link. Please use Excel to create the sample data instead of the manual input method share the xlsx as well.

 

Describe the expected results based on the sample you provide.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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