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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
AnalystDiogenes
Regular Visitor

Text.Combine with multiple each if statements

I have a bunch of tables without any keys in them, and I want to create the same key for each combination of certain columns. I also need to make this straightforward for someone else to do when adding a new table that has the same columns.

So I have [CAH level subject], [Level of Study], [Mode of study] and [Academic year]. (All text columns).
Simply concatinating them all would be a really big field, so I want to use standard abbreviations when I create this key.

In the case of CAH level subject, and Academic Year, I can just extract particular characters from the text before the relevant delimiter to get all the information I need. With Level of Study, I want to turn a long description into a two or three character abbreviation.

 

Here's what I came up with:

= Table.AddColumn(#"Changed Type1", "Key",

each Text.Combine({

Text.BeforeDelimiter([CAH level subject], " "),

":",

each if [Mode of study] = "Full-time" then "FT"

else if [Mode of study] = "Part-time" then "PT"

else null,

":",

each if [Level of study] = "Postgraduate (research)" then "PGR"

else if [Level of study] = "Postgraduate (taught)" then "PGT"

else if [Level of study] = "First degree" then "1st"

else if [Level of study] = "Other undergraduate" then "oUG"

else null,

":",

Text.BeforeDelimiter([Academic Year], "/")

}),

type text)

 

This doesn't work, because ethe each if commands are functions (albeit ones that return text strings), and the Text.Combine wants it's arguments to be text, not functions.

 

Is there a way of having one function do this, so that the future person adding tables can just copy and past the formula when adding a column to new tables?

3 ACCEPTED SOLUTIONS
Anonymous
Not applicable

You might be able to just remove all but the first "each".

 

--Nate

View solution in original post

Omid_Motamedise
Super User
Super User

rewrite it as below


Table.AddColumn(
  #"Changed Type1", 
  "Key", 
  each Text.Combine(
    {
      Text.BeforeDelimiter([CAH level subject], " "), 
      ":", 
      if [Mode of study] = "Full-time" then
        "FT"
      else if [Mode of study] = "Part-time" then
        "PT"
      else
        null, 
      ":", 
      if [Level of study] = "Postgraduate (research)" then
        "PGR"
      else if [Level of study] = "Postgraduate (taught)" then
        "PGT"
      else if [Level of study] = "First degree" then
        "1st"
      else if [Level of study] = "Other undergraduate" then
        "oUG"
      else
        null, 
      ":", 
      Text.BeforeDelimiter([Academic Year], "/")
    }
  ), 
  type text
)
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

View solution in original post

Anonymous
Not applicable

Hi @AnalystDiogenes ,

Please try this:

Text.Combine(
    {
        Text.BeforeDelimiter([CAH level subject], " "), ":",
        if [Mode of study] = "Full-time" then "FT" 
        else if [Mode of study] = "Part-time" then "PT" 
        else null, ":",
        if [Level of study] = "Postgraduate (research)" then "PGR" 
        else if [Level of study] = "Postgraduate (taught)" then "PGT" 
        else if [Level of study] = "First degree" then "1st" 
        else if [Level of study] = "Other undergraduate" then "oUG" 
        else null, ":",
        Text.BeforeDelimiter([Academic year], "/")
    }
)

And the final output is as below:

vjunyantmsft_0-1728892896138.png


Here is the whole M code in the Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY3LCsIwEEV/ZZiVhYo2zQ/4oFt1HbIYzJAUaoXJ5P9tC1Yo7i7cczjO4QkarLErw7DX/sXTvr+zRqFQSBl2wplJnqmaHnM09vBo0NcOz2BmlkT/e0olJv1ZZrEu0G5qXS9ZIXAU5hVuF/gKdpO4aWKBMgaWb2l1LHr/AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"CAH level subject" = _t, #"Mode of study" = _t, #"Level of study" = _t, #"Academic year" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CAH level subject", type text}, {"Mode of study", type text}, {"Level of study", type text}, {"Academic year", type text}}),
    AddColumn = Table.AddColumn(#"Changed Type", "Key", each Text.Combine(
    {
        Text.BeforeDelimiter([CAH level subject], " "), ":",
        if [Mode of study] = "Full-time" then "FT" 
        else if [Mode of study] = "Part-time" then "PT" 
        else null, ":",
        if [Level of study] = "Postgraduate (research)" then "PGR" 
        else if [Level of study] = "Postgraduate (taught)" then "PGT" 
        else if [Level of study] = "First degree" then "1st" 
        else if [Level of study] = "Other undergraduate" then "oUG" 
        else null, ":",
        Text.BeforeDelimiter([Academic year], "/")
    }
))
in
    AddColumn


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

View solution in original post

4 REPLIES 4
slorin
Super User
Super User

Hi, @AnalystDiogenes 

Another solution with 2 records and Record.FieldOrDefault

 

Mode_of_study = Record.FromList({"FT", "PT"}, {"Full-time","Part-time"})
Level_of_study = Record.FromList({"PRG", "PGT", "1st", "oUG"}, {"Postgraduate (research)", "Postgraduate (taught)", "First degree", "Other undergraduate"})

 then add column

Text.Combine({
Text.BeforeDelimiter([CAH level subject], " "), ":",
Record.FieldOrDefault(Mode_of_study, [Mode of study]), ":",
Record.FieldOrDefault(Level_of_study, [Level of study]), ":",
Text.BeforeDelimiter([Academic year], "/")
})

 Stéphane

Anonymous
Not applicable

Hi @AnalystDiogenes ,

Please try this:

Text.Combine(
    {
        Text.BeforeDelimiter([CAH level subject], " "), ":",
        if [Mode of study] = "Full-time" then "FT" 
        else if [Mode of study] = "Part-time" then "PT" 
        else null, ":",
        if [Level of study] = "Postgraduate (research)" then "PGR" 
        else if [Level of study] = "Postgraduate (taught)" then "PGT" 
        else if [Level of study] = "First degree" then "1st" 
        else if [Level of study] = "Other undergraduate" then "oUG" 
        else null, ":",
        Text.BeforeDelimiter([Academic year], "/")
    }
)

And the final output is as below:

vjunyantmsft_0-1728892896138.png


Here is the whole M code in the Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY3LCsIwEEV/ZZiVhYo2zQ/4oFt1HbIYzJAUaoXJ5P9tC1Yo7i7cczjO4QkarLErw7DX/sXTvr+zRqFQSBl2wplJnqmaHnM09vBo0NcOz2BmlkT/e0olJv1ZZrEu0G5qXS9ZIXAU5hVuF/gKdpO4aWKBMgaWb2l1LHr/AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"CAH level subject" = _t, #"Mode of study" = _t, #"Level of study" = _t, #"Academic year" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"CAH level subject", type text}, {"Mode of study", type text}, {"Level of study", type text}, {"Academic year", type text}}),
    AddColumn = Table.AddColumn(#"Changed Type", "Key", each Text.Combine(
    {
        Text.BeforeDelimiter([CAH level subject], " "), ":",
        if [Mode of study] = "Full-time" then "FT" 
        else if [Mode of study] = "Part-time" then "PT" 
        else null, ":",
        if [Level of study] = "Postgraduate (research)" then "PGR" 
        else if [Level of study] = "Postgraduate (taught)" then "PGT" 
        else if [Level of study] = "First degree" then "1st" 
        else if [Level of study] = "Other undergraduate" then "oUG" 
        else null, ":",
        Text.BeforeDelimiter([Academic year], "/")
    }
))
in
    AddColumn


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

Omid_Motamedise
Super User
Super User

rewrite it as below


Table.AddColumn(
  #"Changed Type1", 
  "Key", 
  each Text.Combine(
    {
      Text.BeforeDelimiter([CAH level subject], " "), 
      ":", 
      if [Mode of study] = "Full-time" then
        "FT"
      else if [Mode of study] = "Part-time" then
        "PT"
      else
        null, 
      ":", 
      if [Level of study] = "Postgraduate (research)" then
        "PGR"
      else if [Level of study] = "Postgraduate (taught)" then
        "PGT"
      else if [Level of study] = "First degree" then
        "1st"
      else if [Level of study] = "Other undergraduate" then
        "oUG"
      else
        null, 
      ":", 
      Text.BeforeDelimiter([Academic Year], "/")
    }
  ), 
  type text
)
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
Anonymous
Not applicable

You might be able to just remove all but the first "each".

 

--Nate

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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