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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
LT4RFF
Helper I
Helper I

Very quick 'IF function' question

Hello,

 

I have a column with multiple accounts.

On each of those accounts I am responsible for a percentage.

 

I would like to create a separate column and have a formula 'IF account = B1234 then % = 25' 'IF account = B5678 then % = 50%'

 

So a new column with the percentages running down corresponding to each account.

 

What formula do I enter to do that please?

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

If you only have a few accounts to add the percentage, the the formula provided by @PwerQueryKees should be sufficient.

 

If you have a large number of accounts to consider, you would be better off creating a table of accounts vs reponsible percentages which you could then refer to for your Custom column.

 

Paste the below code into the Advanced Editor to see how that would work. In your real data, you would create the Responsible and Data tables separately.

 

let

//sample table of responsibility
    #"Responsible Percent" = Table.Buffer(Table.FromRecords({
        [Account="B1234", Percent=0.25],
        [Account="B5678", Percent=0.50]},
        type table[Account=text, Percent=Percentage.Type])),

//sample data table
    Source = Table.FromRecords({
        [Account="B1234"],
        [Account="B5678"],
        [Account="A9876"],
        [Account="A1579"],
        [Account="C1234"]
        },type table[Account=text]),
    
//Add responsible percent column per account
    #"Added Custom" = Table.AddColumn(Source, "Responsible Percentage", 
        each 
            try Table.SelectRows(#"Responsible Percent",(r)=>
                r[Account]=[Account])[Percent]{0} 
            otherwise 0, Percentage.Type)
in
    #"Added Custom"

 

Responsible Table

ronrsnfld_0-1729647113325.png

Results

ronrsnfld_1-1729647148039.png

 

 

 

View solution in original post

9 REPLIES 9
ronrsnfld
Super User
Super User

If you only have a few accounts to add the percentage, the the formula provided by @PwerQueryKees should be sufficient.

 

If you have a large number of accounts to consider, you would be better off creating a table of accounts vs reponsible percentages which you could then refer to for your Custom column.

 

Paste the below code into the Advanced Editor to see how that would work. In your real data, you would create the Responsible and Data tables separately.

 

let

//sample table of responsibility
    #"Responsible Percent" = Table.Buffer(Table.FromRecords({
        [Account="B1234", Percent=0.25],
        [Account="B5678", Percent=0.50]},
        type table[Account=text, Percent=Percentage.Type])),

//sample data table
    Source = Table.FromRecords({
        [Account="B1234"],
        [Account="B5678"],
        [Account="A9876"],
        [Account="A1579"],
        [Account="C1234"]
        },type table[Account=text]),
    
//Add responsible percent column per account
    #"Added Custom" = Table.AddColumn(Source, "Responsible Percentage", 
        each 
            try Table.SelectRows(#"Responsible Percent",(r)=>
                r[Account]=[Account])[Percent]{0} 
            otherwise 0, Percentage.Type)
in
    #"Added Custom"

 

Responsible Table

ronrsnfld_0-1729647113325.png

Results

ronrsnfld_1-1729647148039.png

 

 

 

Hello,

Apologies for the delay in resonding.

I think your answer is the best way forward for me as it means I can add more accounts to that list every year.

 

With that said I have got myself in a tangle trying to follow your answer.

 

I have now created a separate table in a separate excel spreadhseet with the account name and it's corresponding percentage.

 

AccountPercent
B0429BALT25.00%
B0429BAAB25.00%
B0507NALB4730024A 
B0429BARBT50.00%
B0429BA2100128 
B0429BASBT50.00%
B0429BAPE100.00%
B2429BWMB100.00%

 

That is now saved as a separate spreadhseet.

 

and now I'm stuck.

 

What code am I entering?  How do I get this spreadsheet into my power query?

In Excel, you would

  1. Select a cell in the table
  2. Navigate Data=>Get & Transform=>From Table/Range

And then in your main query, you would refer to it by the name you see in either side panel that refers to that query (replacing #"Responsible Percent"). Or you could just rename that query #"Responsible Percent".

Omid_Motamedise
Super User
Super User

What is B1234 , if they are text you should put them inside ""

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
LT4RFF
Helper I
Helper I

No I want to do this in Power Query.

 

Get the data in and transform it.

Then @PwerQueryKees has helped with the solution but kindly note that Power Query is CASE sensitive. You must be it the same way you have it in your data or else you may not get your expected output.

PwerQueryKees
Super User
Super User

No swith in Power Query.

so you formula would use in the add column dialog would be:

= if account = "B1234" then 0.25 else if account = "B5678" then 0.50 else 0

Ohh...I thought the formula was to be used in Excel.

Oluwapelumi
Advocate II
Advocate II

You can use SWITCH function in Excel.

=SWITCH(D4, "B5678", "25%", "B1234", "50%") or =SWITCH(D4:D25, "B5678", "25%", "B1234", "50%")

You can use either of the formula above. For the first one, you select the first row under the column while for the second, you highlight all the rows under the column at once but this will make it an array.

Kindly let me know if this help.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.