cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
THEG72 Member
Member

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

Accepted Solutions
ChandeepChhabra Established Member
Established Member

Re: Calculated Column with numeric field and text field

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

 

thanks

View solution in original post

Super User
Super User

Re: Calculated Column with numeric field and text field

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
ChandeepChhabra Established Member
Established Member

Re: Calculated Column with numeric field and text field

@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

 

THEG72 Member
Member

Re: Calculated Column with numeric field and text field

@ChandeepChhabra 

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

 

ChandeepChhabra Established Member
Established Member

Re: Calculated Column with numeric field and text field

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

 

thanks

View solution in original post

Super User
Super User

Re: Calculated Column with numeric field and text field

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

THEG72 Member
Member

Re: Calculated Column with numeric field and text field

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

THEG72 Member
Member

Re: Calculated Column with numeric field and text field

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

Super User
Super User

Re: Calculated Column with numeric field and text field

What would you want the result to be?


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

Re: Calculated Column with numeric field and text field

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

Highlighted
THEG72 Member
Member

Re: Calculated Column with numeric field and text field

@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
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

New Badges

Incoming: New and Improved Badges

Exciting news: We've given our badges an overhaul and added brand news ones.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 150 members 1,458 guests
Please welcome our newest community members: