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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
Anonymous
Not applicable

Comparing next row data and calculating MOD

Hi,

 

I am trying to compare the value of Product ID with the next cell in the table and performing some If conditions. But my logic doesn't work.

 

 

link to my file-- https://www.dropbox.com/s/grxqnjtmyczhbdl/Sample%20file.pbix?dl=0

 

Here is my Code

Column = Var CurentPID =Sheet1[Product ID]
Var NextPID=Sheet1[Product ID]+1
Var CurrentNumber =Sheet1[Number]
Var NextNumber=Sheet1[Number]+1

Return
If(Sheet1[Product ID]=LOOKUPVALUE(Sheet1[Product ID],Sheet1[Index],Sheet1[Index]+1),
if(or(CurrentNumber=1,NextNumber=1),"NA",
if(Mod(CurrentNumber,NextNumber)=0,"OK","FALSE"))
)

 

My data:

Product IDNumber
1111110000
111111000
111111000
111111
22222500
22222100
222221
333331

 

 

My output with Excel formula =if(B2=1,"na"IF(and(B2=1,B3=1),"na",IF(MOD(B2,B3)=0,"OK","False")))

((However now in Power BI I also want to put a check to see if the product ID in the next cell is same, if not same then if number is 1 then NA))

 

Product IDNumberOutput
1111110000OK
111111000OK
111111000OK
111111na
22222500OK
22222100OK
222221na
333331na

 

Any help is appreciated!

Thanks,

Tejaswi

 

2 ACCEPTED SOLUTIONS

Hello @Anonymous 

This worked for me on your sample data, give it a try.

Column = 
VAR _PreviousAmount = 
    CALCULATE(
        SUM ( Sheet1[Number] ),
        ALLEXCEPT ( Sheet1, Sheet1[Product ID] ),
        Sheet1[Index] = Sheet1[Index] - 1)
VAR _MOD = MOD(_PreviousAmount,Sheet1[Number])
RETURN
SWITCH ( 
    TRUE(),
    Sheet1[Number] = 1, "na",
    _MOD = 0, "OK",
    "False"
)

View solution in original post

Gotcha, just change it to this:

Column = 
VAR _Index = 'Sheet1'[Index]
VAR _NextAmount = 
    CALCULATE(
        SUM ( 'Sheet1'[Number] ),
        ALLEXCEPT ( 'Sheet1', 'Sheet1'[Product ID] ),
        'Sheet1'[Index] = _Index + 1)
VAR _MOD = IF ( ISBLANK(_NextAmount),0,MOD('Sheet1'[Number],_NextAmount))
Return
SWITCH ( 
    TRUE(),
    'Sheet1'[Number] = 1, "na",
    _MOD = 0, "OK",
    "False"
)

Mod2.jpg 

View solution in original post

11 REPLIES 11
Anonymous
Not applicable

Hi,

 

@jdbuchanan71@Zubair_Muhammad, @Sean, @TomMartens

 

Any help greatly appreacited!

 

 

-Tejaswi

Hello @Anonymous 

This worked for me on your sample data, give it a try.

Column = 
VAR _PreviousAmount = 
    CALCULATE(
        SUM ( Sheet1[Number] ),
        ALLEXCEPT ( Sheet1, Sheet1[Product ID] ),
        Sheet1[Index] = Sheet1[Index] - 1)
VAR _MOD = MOD(_PreviousAmount,Sheet1[Number])
RETURN
SWITCH ( 
    TRUE(),
    Sheet1[Number] = 1, "na",
    _MOD = 0, "OK",
    "False"
)
Anonymous
Not applicable

HI @jdbuchanan71 ,

 

I found the below issue when I added this code in a actual file.

 When I used this in my actual data , it should appear false if the mod is not zero however it still shows OK.

 

Can you please help me?

 

Capture3.PNG

 

For example : Product ID 44444 , Number is 110 and the next number is 12, the Mod of these two numbers 110/12 is non zero still the output shows OK, Could you please help tweak your code?

 

Thanks,

Tejaswi

@Anonymous 

Try changing it to this

Column = 
VAR _Index = Sheet1[Index]
VAR _PreviousAmount = 
    CALCULATE(
        SUM ( Sheet1[Number] ),
        ALLEXCEPT ( Sheet1, Sheet1[Product ID] ),
        Sheet1[Index] = _Index - 1)
VAR _MOD = MOD(_PreviousAmount,Sheet1[Number])
RETURN
SWITCH ( 
    TRUE(),
    Sheet1[Number] = 1, "na",
    _MOD = 0, "OK",
    "False"
)

The MOD was off because the row index was not getting picked up.

Anonymous
Not applicable

HI @jdbuchanan71 ,

 

Thanks for your reply!

 

But now it shows FALSE for all the OKs aswell.

 

Capture5.PNG

This does not match what my results are:

Column = 
VAR _Index = 'Sheet1'[Index]
VAR _PreviousAmount = 
    CALCULATE(
        SUM ( 'Sheet1'[Number] ),
        ALLEXCEPT ( 'Sheet1', 'Sheet1'[Product ID] ),
        'Sheet1'[Index] = _Index - 1)
VAR _MOD = MOD(_PreviousAmount,'Sheet1'[Number])
RETURN
SWITCH ( 
    TRUE(),
    'Sheet1'[Number] = 1, "na",
    _MOD = 0, "OK",
    "False"
)

Mod.jpg

Anonymous
Not applicable

@jdbuchanan71 ,

 

yes I made a mistake while copy pasting and renaming the the actual field name.  I get the results what you get .

 

However can we have the highest number as false. In you case it shows 12 as false. I want highest number 110 false if the mod is non zero and 12 as OK

 

Appreacite all your kind help!

 

Thanks,

Tejaswi

Gotcha, just change it to this:

Column = 
VAR _Index = 'Sheet1'[Index]
VAR _NextAmount = 
    CALCULATE(
        SUM ( 'Sheet1'[Number] ),
        ALLEXCEPT ( 'Sheet1', 'Sheet1'[Product ID] ),
        'Sheet1'[Index] = _Index + 1)
VAR _MOD = IF ( ISBLANK(_NextAmount),0,MOD('Sheet1'[Number],_NextAmount))
Return
SWITCH ( 
    TRUE(),
    'Sheet1'[Number] = 1, "na",
    _MOD = 0, "OK",
    "False"
)

Mod2.jpg 

Anonymous
Not applicable

@jdbuchanan71 ,

 

Sorry  I made a typo while copy pasting the data. 

Its close enough but can we have the 110 as False? The highest number to be false and other is OK

 

Capture4.PNG

Anonymous
Not applicable

Thanks @jdbuchanan71  for your quick reply and solutions.

 

This worked as expected.

 

 

Thanks,

Tejaswi

kentyler
Solution Sage
Solution Sage

I think the standard advice in Power BI is to do these kinds of calculations in Excel. Excel is made for doing things involving adjacent cells and rows. Power BI is not. You can do it in Power BI but its complicated and you end up with slow measures.





Did this post answer your question? Mark it as a solution so others can find it!

Help when you know. Ask when you don't!




Join the conversation at We Talk BI find out more about me at Slow BI


Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.