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
foyiq
Advocate I
Advocate I

Subtract values from different rows

 

Dear contributors,

I have a query result table which includes data from a folder in a following format. It looks approximately as follows:

 

QuarterYearStatusItemValue
Q12016budgetexpense itemx
Q22016budgetexpense itemx
Q32016budgetexpense itemx
Q42016budgetexpense itemx
Q12016actualexpense itemx
Q22016actualexpense itemx
Q32016actualexpense itemx
Q42016actualexpense itemx
Q12017budgetexpense itemx
Q22017budgetexpense itemx
Q32017budgetexpense itemx
Q42017budgetexpense itemx
Q12017actualexpense itemx
Q22017actualexpense itemx
Q32017actualexpense itemx
Q42017actualexpense itemx

There are many other columns, which I took out for simplicity; many separate expense items, from which I will take only some in visuals later on.

 

The problem is that Actual values come in cumulative terms, whereas in Budget values come 'cleaned'. What I need, thus, is to be able to compare between Budget and Actual via respective visualisations (which I will do further by myself).
Now the problem is to get the Actual net figure for Q2 and Q3 inside a year (Q1 is already clean, and Q4 is yearly data). I am not being able to use correct combination of filters for calculate, as later in visuals I am using date filters (I have a separate date calendar, which I will relate to this one). Maybe it is possible to accomplish the task on a query level, but I am having trouble with this way, too.

The matter is quite urgent and your help would be highly appreciated.

Thank you beforehand.

1 ACCEPTED SOLUTION
v-ljerr-msft
Employee
Employee

Hi @foyiq,

 

According to your description above, you should be able to use the formula below to create a new calculate column in your table to get the Actual net figure for Q2 and Q3 inside a year. Smiley Happy

 

Value2 =
VAR pq =
    IF ( Table1[Quarter] = "Q2", "Q1", "Q2" )
RETURN
    IF (
        Table1[Status] = "budget"
            || Table1[Quarter] = "Q1"
            || Table1[Quarter] = "Q4",
        Table1[Value],
        Table1[Value]
            - CALCULATE (
                MAX ( Table1[Value] ),
                FILTER (
                    ALL ( Table1 ),
                    Table1[Year] = EARLIER ( Table1[Year] )
                        && Table1[Quarter] = pq
                )
            )
    )

Note: Just replace "Table1" with your real table name.

 

c1.PNG

 

Regards

View solution in original post

4 REPLIES 4
v-ljerr-msft
Employee
Employee

Hi @foyiq,

 

According to your description above, you should be able to use the formula below to create a new calculate column in your table to get the Actual net figure for Q2 and Q3 inside a year. Smiley Happy

 

Value2 =
VAR pq =
    IF ( Table1[Quarter] = "Q2", "Q1", "Q2" )
RETURN
    IF (
        Table1[Status] = "budget"
            || Table1[Quarter] = "Q1"
            || Table1[Quarter] = "Q4",
        Table1[Value],
        Table1[Value]
            - CALCULATE (
                MAX ( Table1[Value] ),
                FILTER (
                    ALL ( Table1 ),
                    Table1[Year] = EARLIER ( Table1[Year] )
                        && Table1[Quarter] = pq
                )
            )
    )

Note: Just replace "Table1" with your real table name.

 

c1.PNG

 

Regards

MarcelBeug
Community Champion
Community Champion

If I understand correctly, for actuals in Q2 and Q3 you need the value minus the previous value (if available, maybe some data start with Q2 or Q3), otherwise you need the value.

 

Solution in Power Query; a prerequisite is that the data is already sorted on quarters:

 

let
    Source = Input,
    Grouped = Table.Group(Source, {"Year", "Status", "Item"}, {{"AllData", each _, type table}}),
    AddedNetValue =
        Table.TransformColumns(Grouped,
                               {"AllData",
                                 (Data) =>
                                    let
                                        Indexed = Table.AddIndexColumn(Data, "Index", -1, 1),
                                        AddedNetValue = Table.AddColumn(Indexed,
                                                                        "Net value",
                                                                        each if [Status] = "actual" and
                                                                                -1 < [Index] and
                                                                                -1 < List.PositionOf({"Q2","Q3"},[Quarter])
                                                                             then [Value] - Indexed[Value]{[Index]}
                                                                             else [Value])
                                    in
                                        AddedNetValue
                                }),
    Expanded = Table.ExpandTableColumn(AddedNetValue, "AllData", {"Quarter", "Value", "Net value"}, {"Quarter", "Value", "Net value"}),
    Reordered = Table.ReorderColumns(Expanded,{"Quarter", "Year", "Status", "Item", "Value", "Net value"})
in
    Reordered
Specializing in Power Query Formula Language (M)

@MarcelBeug Thanks so much for quick response!

Sorry for being such a rookie, but in this exact format, unfortunately, I am not able to reproduce your solution. May I include the more inclusive sample, where all the columns are shown, and note that I already have it in a query mode, so i can just continue reshaping there.

FileNamePeriodQuarterYearStatusStatus2EntityEntityCodeEntityFullNameRateItemCodeItemNameValueSlicer
PL budget 1Q 2017.xlsQ1 2017Q12017BudgetQuarterlyshortname11111fullname3.51156Expenses6548.35Budget|Quarterly|shortname
PL actual 1Q 2017.xlsQ1 2017Q12017ActualQuarterlyshortname11111fullname3.371156Expenses9510.8Actual|Quarterly|shortname
PL actual 2Q 2017.xlsQ2 2017Q22017ActualQuarterlyshortname11111fullname3.11156Expenses16410.8Actual|Quarterly|shortname



Each file represents a period (quarter and year) and status (budget and actual). Inside are different items.
Do I need to sort it so that each next row shows next quarter (you told about prerequisite condition)?

I guess I can't just get this query result in excel format, so as to make it a source for new query, as you have shown.

 

Thank you!

MarcelBeug
Community Champion
Community Champion

Just to be sure, you can add a sort step after the inner let:

 

                                        Sorted = Table.Sort(Data, {"Quarter"}),
                                        Indexed = Table.AddIndexColumn(Sorted, "Index", -1, 1),

 

Power Query is case sensitive so you need to change "actual" (based on your first example data) in the code to "Actual" (based on your last example data).

 

My suggestion would be to delete the last 2 steps and recreate them with your real data.

Expand is just the double-arrow button at the right in the header of column "AllData": there you have to choose your columns.

Reorder: you can just drag columns and the reorder code will be created for you,

As finishing touch I renamed the steps so the code looks better in the Advanced Editor.

 

Actually I created my code in Excel and the output is in Excel, no problem (if you have the right version: I see xls's instead of xlsx's as file extension).

Specializing in Power Query Formula Language (M)

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.