Forum Discussion
Switch statement in power query
Can some one help me build switch statement in power query
- Anonymous5 years ago
the code you indicate I believe is DAX (which I don't know). Try to see if this is okay for you. If the control conditions are different (not "simple" equalities) it is necessary to change the setting. But in case you should give specific examples of which situation you need to manage.
Hi Anonymous
There is no built-in Switch function in Power Query. In additon to the custom Switch function, you can also achieve this by adding a conditional column easily. It will generate an IF statement like below.
if [Segment1] = 1 then "ABC" else if [Segment1] = 54 then "FGH" else if [Segment1] = 56 then "JKL" else if [Segment1] = 101 then "BNM" else nullRegards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.
12 Replies
- PL-JordellsRegular Visitor
Hi,
I know this is an older thread, but I have a personal solution that I have been preferring to use in several situations.
I use a "Record.Field" function around a custom record of my possible cases:
each Record.Field([Case1="Output1", Case2="Output2", Case3="Output3"], [INPUT])
If the input exactly matches any of the record's field names, then it will give that field's value.
This allows you to only need to have the input in the code once, and not need to have all the "Else If" and "Then" statements within the code.
There are some limitations to this approach (some of which I may be forgetting at this time), but here are a few extra tips with this approach which can be combined as needed:
- You can put this within a "try otherwise" statement to allow a default value when there is no exact field match:
each try Record.Field([Case1="Output1", Case2="Output2", Case3="Output3"], [INPUT]) otherwise "Default Output"
- To allow case-insensitivity, you can use a "Text.Upper" or "Text.Lower" function on your input, then have all Case fields match that casing:
each Record.Field([case1="Output1", case2="Output2", case3="Output3"], Text.Lower([INPUT]))
- If you might have number values instead of text for any/all of your input, you can use the "Text.From" function on your input:
each Record.Field([101="Output1", 102="Output2", 103b="Output3"], Text.From([INPUT]))
- Note: You can have spaces in the record's field names; only the spaces between characters will be considered part of the field name:
each Record.Field([Case Test 1="Output1", Case Test 2="Output2", Case Test 3="Output3"],[INPUT])
I hope this is helpful to someone! (or my future self 😁)
EDIT:
I noticed after posting this, that you had posted a reply with your specific use case, so here is my solution with your specific details:
each Record.Field([1="ABC", 54="FGH", 56="JKL", 101="BNM"],Text.From([#"Discoverer-Receivables"][Segment1]))
(This example assumes "Discover-Receivables" is a record column where each record has a "Segment1" field.)
- limwongNew Member
FYI if you have leading or trailing spaces or special characters in the field names then wrap it in #" "
[#" Case Test 1 - 1.xlsx ","Output1",#" Case Test 2 - 2.xlsx ","Output2"....]each Record.Field([Case Test 1="Output1", Case Test 2="Output2", Case Test 3="Output3"],[INPUT])
- Michel_SoaresFrequent Visitor
Thank you, this helped me to keep my code simple in cases that the information is static.
- You can put this within a "try otherwise" statement to allow a default value when there is no exact field match:
- ImkeFCommunity Champion
Hi Anonymous ,
I've created a function for this a while ago here: A generic SWITCH-function for the query editor in Power BI and Power Query – The BIccountantJust be aware, that it doesn't cover Switch-true-statements.
- AnonymousNot applicable
waiting for you to better clarify what you expect, I propose this scheme.
let /* switch( c ) { case 'A': capital_a++; case 'a': letter_a++; default : total++; } */ switch= (c) => if c= "A" then "add_One_A" else if c="a" then "add_One_a" else "add_nOne_a" in switch- AnonymousNot applicableSWITCH (TRUE (),'Discoverer-Receivables'[Segment1] = 1, "ABC",'Discoverer-Receivables'[Segment1] = 54, "FGH",'Discoverer-Receivables'[Segment1] = 56, "JKL",'Discoverer-Receivables'[Segment1] = 101, "BNM")Looking to implement this logic in power query
- v-jingzhangCommunity Support
Hi Anonymous
There is no built-in Switch function in Power Query. In additon to the custom Switch function, you can also achieve this by adding a conditional column easily. It will generate an IF statement like below.
if [Segment1] = 1 then "ABC" else if [Segment1] = 54 then "FGH" else if [Segment1] = 56 then "JKL" else if [Segment1] = 101 then "BNM" else nullRegards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.
- AnonymousNot applicable
You need to provide more information in order for us to help.