cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

Multiply column with several other columns individually

Hi,

 

I have 10 columns that I want to multiply with a column called FX.

 

For example, I have columns:

 

1. Sales

2. Salaries

3. Tax

4. FX rate

 

I want to new columns equal to:

 

1. Sales*FX rate

2. Salaries*FX rate

3. Tax*FX rate

 

 

What would be the most elegant way to do this? I am open to using power query or dax. I know how to do this in other programming languages (i.e. a simple loop) but am new to power BI, any help would be appreciated.

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Thanks, is there a way to do this programmtically:

 

For each col in my columns:

   new_col = col * multiplication column

   add new_col to table

View solution in original post

HotChilli
Super User
Super User

I've had a go.  You'll have to incorporate this into your Power Query code with your previous step names etc.

#"stepNext" = List.Accumulate(Table.ColumnNames(#"Inserted Multiplication"), #"Inserted Multiplication", 
    (state, current) => 
      Table.AddColumn(state, "XX " & current, each [Multiplication] * Record.Field(_, current), Int64.Type) )

There's no foreach in M, it's functional programming so we pass a list of column names to the function List.Accumulate and say what we want to do with those column names.

 

View solution in original post

4 REPLIES 4
HotChilli
Super User
Super User

I've had a go.  You'll have to incorporate this into your Power Query code with your previous step names etc.

#"stepNext" = List.Accumulate(Table.ColumnNames(#"Inserted Multiplication"), #"Inserted Multiplication", 
    (state, current) => 
      Table.AddColumn(state, "XX " & current, each [Multiplication] * Record.Field(_, current), Int64.Type) )

There's no foreach in M, it's functional programming so we pass a list of column names to the function List.Accumulate and say what we want to do with those column names.

 

v-xulin-mstf
Community Support
Community Support

Hi @Anonymous

 

You can create three measures as:

Sales*FX rate=
Calculate
   (
     max('table'[Sales])*max('table'[FX rate]),
     'table'
    )
Salearires*FX rate=
Calculate
   (
     max('table'[Salearires])*max('table'[FX rate]),
     'table'
    )
Tax*FX rate=
Calculate
   (
     max('table'[Tax])*max('table'[FX rate]),
     'table'
    )

 

Best Regards,

Link

 

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

Anonymous
Not applicable

Thanks, is there a way to do this programmtically:

 

For each col in my columns:

   new_col = col * multiplication column

   add new_col to table

HotChilli
Super User
Super User

You've basically shown how to do it right there.  Just go to Power Query->add a new column ,either custom column and add code manually or

use the interface (select 2 columns -> use the ribbon in the (standard->multiply) - in the 'From number' section.

You'll work it out

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.