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
alfertab
Helper I
Helper I

Calculate de division of one row vs other row with conditionals

Hi everybody, I'm having some troubles with a calculation I need to do and I would like to know if someone knows what to do.

 

Basically I have the following table with daily data of different countries.

 

CountryDateIncomeOutcomePercentage 
England29/04/2023$8,000$6,000 
China29/04/2023$9,500$7,500 
Mexico29/04/2023$10,000$8,000 
England30/04/2023$7,500$9,000 
China30/04/2023$8,500$7,000 
Mexico30/04/2023$10,500$9,000 
England01/05/2023$11,000$8,000 
China01/05/2023$10,500$7,500 
Mexico01/05/2023$14,500$11,000 

 

What I need to calculate is the Percentage of the Outcome compared to the next day's income; in case there is no data for the next day, the result of the outcome/income for that same day must be calculated. For example, the value for England(29/04/2023) must be calculated as following: 

alfertab_0-1708639708748.png

Another example could be the value for China (30/04/2023):

alfertab_1-1708639850208.png

The last example could be the value for Mexico (01/05/2023), where we don't have data for the next day:

alfertab_2-1708639995898.png

 

The calculation needs to be an extra column on the table. At first I was using Index in Power Query, but the problem is that in some future there may be data from more countries and I think using index there would be errors. Does anyone know how to do it? It could be as a column calculated in Power Query or in Power BI. Thanks. 

2 ACCEPTED SOLUTIONS
lbendlin
Super User
Super User

lbendlin_1-1708651622217.png

 

Percentage =
VAR d = [Date]
VAR c = [Country]
RETURN
    DIVIDE (
        [Outcome],
        COALESCE (
            CALCULATE (
                SUM ( 'Table'[Income] ),
                ALL ( 'Table' ),
                'Table'[Country] = c,
                'Table'[Date] = d + 1
            ),
            [Income]
        ),
        0
    )

View solution in original post

v-weiyan1-msft
Community Support
Community Support

Hi @alfertab ,

 

@lbendlin nice method! Thank you, for your quick response and the solution provided.
And based on the sample and description you provided, you can also try code as below to create a Calculated column.

Percentage = 
VAR CurrentCountry = 'Table'[Country]
VAR CurrentOutCome = 'Table'[Outcome]
VAR NextDate = NEXTDAY('Table'[Date])
VAR NextIncome =
    CALCULATE (
        MAX ( 'Table'[Income] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Country] = CurrentCountry
                && 'Table'[Date] = NextDate
        )
    )
RETURN
    IF (
        ISBLANK ( NextIncome ),
        DIVIDE ( CurrentOutCome, 'Table'[Income] ),
        DIVIDE ( CurrentOutCome, NextIncome )
    )

Result is as below.

vweiyan1msft_0-1708655550316.png


Best Regards,
Yulia Yan


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

3 REPLIES 3
alfertab
Helper I
Helper I

@lbendlin @v-weiyan1-msft Thanks both of you for the answers :D. Both methods were usefull and worked. Thanks a lot.

v-weiyan1-msft
Community Support
Community Support

Hi @alfertab ,

 

@lbendlin nice method! Thank you, for your quick response and the solution provided.
And based on the sample and description you provided, you can also try code as below to create a Calculated column.

Percentage = 
VAR CurrentCountry = 'Table'[Country]
VAR CurrentOutCome = 'Table'[Outcome]
VAR NextDate = NEXTDAY('Table'[Date])
VAR NextIncome =
    CALCULATE (
        MAX ( 'Table'[Income] ),
        FILTER (
            ALL ( 'Table' ),
            'Table'[Country] = CurrentCountry
                && 'Table'[Date] = NextDate
        )
    )
RETURN
    IF (
        ISBLANK ( NextIncome ),
        DIVIDE ( CurrentOutCome, 'Table'[Income] ),
        DIVIDE ( CurrentOutCome, NextIncome )
    )

Result is as below.

vweiyan1msft_0-1708655550316.png


Best Regards,
Yulia Yan


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

lbendlin
Super User
Super User

lbendlin_1-1708651622217.png

 

Percentage =
VAR d = [Date]
VAR c = [Country]
RETURN
    DIVIDE (
        [Outcome],
        COALESCE (
            CALCULATE (
                SUM ( 'Table'[Income] ),
                ALL ( 'Table' ),
                'Table'[Country] = c,
                'Table'[Date] = d + 1
            ),
            [Income]
        ),
        0
    )

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!

November Carousel

Fabric Community Update - November 2024

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

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.