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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
THEG72
Helper V
Helper V

Calculated Column with numeric field and text field

I have the following table data and would like to make  a custom column that will have 2 possible outputs "Cash Inflow" or "Cash Outflow" as shown in the formula required section below.

 

There are two groups of transactions as shown in the FinanceGroup. Some inflows are positive so there are revenue items and some and negative representing selling costs. The same scenario on the outflows side but again a positive is an outflow and negative is a refund or return of cashflow costs.

 

What is the best method to achieve this result efficiently using a M custom column?

 

What is the correct method to first reference the Text field in the finance group and then work out if the value is positive or negative so the amount can be correctly classified? 

 

I used this code below, but just looking to see what is best method.

 

if [FinanceGroup] = "Revenue" and [Value] > 0 then "Cash Inflow"
else if [FinanceGroup] = "Revenue" and [Value] < 0 then "Cash Outlfow"
else if [FinanceGroup] = "Costs" and [Value] > 0 then "Cash Outlfow"
else if [FinanceGroup] = "Costs" and [Value] < 0 then "Cash Inflow" else "Nil Cashflow"

 

Test.PNG

2 ACCEPTED SOLUTIONS

@THEG72 If my reply helped, could you mark it as a solution?

 

thanks

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Finance Group", type text}, {"Value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Finance Group]="Revenue" then if [Value]>0 then "Cash Inflow" else "Cash Outflow" else if [Value]<0 then "Cash Outflow" else "Cash Inflow")
in
    #"Added Custom"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

9 REPLIES 9
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Finance Group", type text}, {"Value", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Finance Group]="Revenue" then if [Value]>0 then "Cash Inflow" else "Cash Outflow" else if [Value]<0 then "Cash Outflow" else "Cash Inflow")
in
    #"Added Custom"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

@Ashish_Mathur  thanks for your M code, what happens if the value is zero? how would you code account for that?

What would you want the result to be?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
ChandeepChhabra
Impactful Individual
Impactful Individual

@THEG72 You can use DAX or M

Please refer to the Excel file for both solutions

 

Capture.png

 

Two quick things

  1. The label "cost" is written in 2 different ways, I have provided for that in DAX and M
  2. Please note that power query is case sensitive and DAX is not

Thanks

 

@ChandeepChhabra 

thanks there is a mistake there is only two codes Revenue or Costs.

 

@THEG72 If my reply helped, could you mark it as a solution?

 

thanks

@ChandeepChhabra  Can you switch statement handle a zero value too ?

@THEG72 No it can't for now. What label do you want in-case it is a zero?

@ChandeepChhabra  I guess this is more for error logic and accounting for all the possibilities of the values. you could output "No Cashflow"

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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