SOLVED

Pivot Table group positive and negative data

Copper Contributor

As you can see in the below image i have created two separate Pivot tables to group the positive and negative values separately.
1. Is there a way to group positive and negative value as sub groups in a single Pivot table instead of two like i have done.

2. Can i use the grand total of positive values and then divide each negative values by the income grand total to get the percentage of the expenses ie. (Each expense)/(Income grand total)*100

This way i can calculate what percentage of my income makes up each of my expenses. 

Rohan110_0-1687317916896.png

 

11 Replies

@Rohan110 

 

When you find a minute, When Array table is updated/ sorted, the adjacent column cells aren't aligned/linked is pending your feedback...

 

Re. your Pivot Table question the following is doable (there's a blank row between Categories for clarity but it can be removed) but not with a classic Pivot Table only, this is a single Pivot though:

 

Sample.png

 

You tagged your thread with Excel, Excel on Mac, Excel on mobile, Power BI. The above is acheivable with Excel >= 2013 on Windows and Power BI but not on Excel on Mac. Interested?

Hi Lz.,
I use Microsoft office 365, with the latest excel version. Please provide your suggestion.

Hi @Rohan110 

 

Sample attached:

#1 Table1 is loaded to Power Query to Unpivot [Expense] & [Income] (kept the default "Attribute" column name - easy to change)

#2 Unpivoted table loaded to the Excel Data Model (Windows only)

#3 A couple of DAX measures (don't ask too much I have very basic knowledge)

Hi L z.
Thanks for your quick response. Could you please let me know how you carried out unpivoting step which resulted in categorizing the data as rows and columns.
Also, my raw data has a single column of income and expenses, income in positive values and expense as negative values. Does this mean i need to split them into separate column, positive and negative and then carryout your method? Appreciate your support on this.
best response confirmed by Rohan110 (Copper Contributor)
Solution

@Rohan110 

 

Attached an example with a single [Amount] column with Positive & Negative values:

 

Sample.png

Edit the query in the attachment and follow the steps in APPLIED STEPS (all done with the UI). Once you've separated your [Income] & [Expense] (the added Custom steps) and removed [Amount] that's no longer necessary, right-click on column [Category] and in the drop-down menu select Unpivot Other Columns

 

IMPORTANT:

- For [Expense] you'll see I used function Number.Abs  to convert to negative [Amount]s to positive. If you want your Expense to show negative in your PivotTable then remove Number.Abs (you will have to adjust the Graph DAX measure)

- When you add Custom Columns [Income] & [Expense] Power Query doesn't auto. Type the columns. Taking ex. [Income] when you create it, it displays exactly as below in PQ formula bar:

Sample2.png

You must edit the formula and add, before ): , type number then Validate:

Sample3.png

You must do the same for the [Expense] column

Reason is: default data Type in Power Query is Type any. Column(s) of Type any loaded to the Excel Data Model will be assigned the Type Text (no Sum, Avg... possible)

 

Hi @Rohan110 

Does no news means good news? (Marking solutions helps people who Search...)

Hi Lz,
Thanks for the useful tip and also taking time in explaining it in very detailed format.

@Lorenzo 

 

Simpler step to categorize the data. But thanks. This helped me to segregate into two different categories. 

 

Rohan110_0-1695458977661.png

 

@Lorenzo

 

There is a problem with this approach. As you can see, with this approach, as transfers has both positive and negative values in the data, it gets listed in both income and expense. With this approach I'm unable to calculate my net income and net expenses. Could you please help with this? 

 

Rohan110_0-1696303061123.png

 

 

can you upload your data and expected result?

@Rohan110 

 

Note quite sure this will do exactly what you expect....: Aggregate the amounts for the Transfers category. Then, if the aggregated amount is >= 0 Transfers will be reported as Income otherwise as Expense

 

let
    Source = Excel.CurrentWorkbook(){[Name="SourceTable"]}[Content],
    ChangedTypes = Table.TransformColumnTypes(Source,{{"Category", type text}, {"Amount", type number}}),
    Transfers = Table.Group(
        Table.SelectRows(ChangedTypes, each ([Category] = "Transfers")),
        {"Category"}, {{"Amount", each List.Sum([Amount]), type nullable number}}
    ),
    OtherCategories = Table.SelectRows(ChangedTypes, each ([Category] <> "Transfers")),
    Combined = Table.Combine({OtherCategories, Transfers}),
    AddedIncome = Table.AddColumn(Combined, "Income", each
        if [Amount] >= 0 then [Amount] else null, type number
    ),
    AddedExpense = Table.AddColumn(AddedIncome, "Expense", each
        if [Amount] < 0 then Number.Abs([Amount]) else null, type number
    ),
    RemovedAmount = Table.RemoveColumns(AddedExpense,{"Amount"}),
    UnpivotedColumns = Table.UnpivotOtherColumns(RemovedAmount, {"Category"}, "Attribute", "Value")
in
    UnpivotedColumns

 

1 best response

Accepted Solutions
best response confirmed by Rohan110 (Copper Contributor)
Solution

@Rohan110 

 

Attached an example with a single [Amount] column with Positive & Negative values:

 

Sample.png

Edit the query in the attachment and follow the steps in APPLIED STEPS (all done with the UI). Once you've separated your [Income] & [Expense] (the added Custom steps) and removed [Amount] that's no longer necessary, right-click on column [Category] and in the drop-down menu select Unpivot Other Columns

 

IMPORTANT:

- For [Expense] you'll see I used function Number.Abs  to convert to negative [Amount]s to positive. If you want your Expense to show negative in your PivotTable then remove Number.Abs (you will have to adjust the Graph DAX measure)

- When you add Custom Columns [Income] & [Expense] Power Query doesn't auto. Type the columns. Taking ex. [Income] when you create it, it displays exactly as below in PQ formula bar:

Sample2.png

You must edit the formula and add, before ): , type number then Validate:

Sample3.png

You must do the same for the [Expense] column

Reason is: default data Type in Power Query is Type any. Column(s) of Type any loaded to the Excel Data Model will be assigned the Type Text (no Sum, Avg... possible)

 

View solution in original post