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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
afkl99
Frequent Visitor

Dividing rows in table

Hi, 

I am new to power bi and am trying to transfer my r code into steps in the power bi query

  • The r code itterates through each row of the data frame and divides the value in row (a) by the value in row (a-1): value_a / value_(a-1)

 

I have a large table with a date and value as follows: 

snip.PNG


I want to create a new column which takes the value of each row and divides it by the value by that of the pervious row -a seemingly easy task. i.e. the calculated column will be 0 for the first row and then 300/400 for the second row and 250/300 for the third row and so on.

 

I have spent hours looking for the solution for this and have come up empty as often the solutions uses functions that are not available in the query when creating custom column 

 

Thank you so much for your help! 

1 ACCEPTED SOLUTION
CahabaData
Memorable Member
Memorable Member

this may help get you on your path - it doesn't do the calculation but does bring in the value from the prior day/row:

terms are generic - first is a calculated column approach:

 

PreDaySessions_Column =

VAR CurrentDay = Table1[Date]

VAR PreDay =

 CALCULATE ( MAX ( Table1[Date] ), FILTER ( Table1, Table1[Date] < CurrentDay ) )

RETURN

 LOOKUPVALUE ( Table1[SessionS], Table1[Date], PreDay )

 

this is a Measure

PreDaySessions_Measure =

VAR PreDay =

  PREVIOUSDAY ( Table1[Date] )

RETURN

 LOOKUPVALUE ( Table1[SessionS], Table1[Date], PreDay )

 

 

www.CahabaData.com

View solution in original post

4 REPLIES 4
CahabaData
Memorable Member
Memorable Member

this may help get you on your path - it doesn't do the calculation but does bring in the value from the prior day/row:

terms are generic - first is a calculated column approach:

 

PreDaySessions_Column =

VAR CurrentDay = Table1[Date]

VAR PreDay =

 CALCULATE ( MAX ( Table1[Date] ), FILTER ( Table1, Table1[Date] < CurrentDay ) )

RETURN

 LOOKUPVALUE ( Table1[SessionS], Table1[Date], PreDay )

 

this is a Measure

PreDaySessions_Measure =

VAR PreDay =

  PREVIOUSDAY ( Table1[Date] )

RETURN

 LOOKUPVALUE ( Table1[SessionS], Table1[Date], PreDay )

 

 

www.CahabaData.com

Thank you! I had tried the first method earlier and it didnt seem to work - but i will try it again 
 
Can you add a measure when using the query? I was under the impression that it had to be done outside the query editor.  

Hi @afkl99,

 

We did it in the data view or report view. You also can try this formula.

 

Column =
VAR currentDate = 'Table1'[Date]
VAR PreDate =
    CALCULATE (
        LASTDATE ( 'Table1'[Date] ),
        FILTER ( 'Table1', 'Table1'[Date] < currentDate )
    )
RETURN
    IF (
        ISBLANK ( PreDate ),
        0,
        DIVIDE (
            'Table1'[Value],
            LOOKUPVALUE ( 'Table1'[Value], Table1[Date], PreDate ),
            0
        )
    )

Dividing rows in table.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

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

Hi @afkl99,

 

Could you please mark the proper answer as solution or share the solution if it's convenient for you? That will be a big help to the others.

 

 

Best Regards!
Dale

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

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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