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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Custom Column: Re-using Parameters

I am very new to Power BI, so forgive my novice question!

 

I know how to create a custom column to convert letter values to number values in a separate column, but there are two problems:

 

  1. The number values are entered as text, and I have to manually tweak the code in the advanced editor to remove the quote marks.
  2. I want to perform the same create column operation on other columns - the only way I have found to do this is to copy the code in advanced editor and tweaking the column references.

I am sure there is a more efficient way of doing this, but I can't quite work it out. Any suggestions? I have a conversion table already set up in an Excel file, if that helps. Thanks!

1 ACCEPTED SOLUTION

Thanks for clarification. You can put a sample file somewhere on internet (e.g. OneDrive) and provide the link.

 

Anyhow, I would add custom columns and use function Text.PositionOf to convert the text to numbers.

As a final step, you can change the column types to numbers. 

 

 

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Challenge Score", each Text.PositionOf("USGEO",[Challenge Grade])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Attainment Score", each Text.PositionOf("USGEO",[Attainment Grade])),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom1",{{"Challenge Score", Int64.Type}, {"Attainment Score", Int64.Type}})
in
    #"Changed Type"

 

Specializing in Power Query Formula Language (M)

View solution in original post

6 REPLIES 6
MarcelBeug
Community Champion
Community Champion

It's hard to understand your situation and requirements.

 

The numbers may be text, but why are there quotation marks?

What is the source of your data: txt/csv files, current workbook, external workbook, other source?

What do you mean with re-using parameters? I don't see any parameters in your story.

Is the conversion table meant to convert the letters to numbers? Or is it just A=1, B=2 etcetera? Or "1" = 1, "2" = 2, etcetera?

Depending on your exact requirements, maybe something like unpivot might be appropriate, but it is impossible to judge based on the limited information provided,

 

Please provide further explanation and preferably a link to a file with representative example data.

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Sorry to appear vague - that's because it's all relatively new to me. Let me try to explain again (I can't share data at the moment, as it is sensitive).

 

I have an Excel file with data drawn from our pupil database, which I have normalised using Power Query. It consists of text columns:

 

Name - text

Challange Grade - text (grades from O, E, G, S and U)

Attainment Grade - text (grades from O, E, G, S and U)

 

I know how to create a custom condtional column to create a new column that converts the challenge grades to numbers (O=4, E=3, G=2, S=1 and U=0), but the code that is generated by the conditional column wizard is still seen as text (because the code generated puts the numbers into inverted commas thus: "4"), so I have to edit this in the advanced query in order for Power BI to treat these values as numbers. That's the first thing.

 

The second issue is duplicating the custom conditional column to create a conversion column for the attaiment grades: I can either set up another conditional column with exactly the same parameters as the one mentioned above, or I can copy the code in the advanced editor window and then tweak the source column and destination column names. What I am interested in knowing is whether there is a more efficient way of doing this. I wondered about a custom function, but am not sure if it will work.

 

Does this make any more sense?

 

I am attaching a very simple representation of what I am after. I hope it helps.

 

EDIT: How do I attach my sample file?

 

Thanks for clarification. You can put a sample file somewhere on internet (e.g. OneDrive) and provide the link.

 

Anyhow, I would add custom columns and use function Text.PositionOf to convert the text to numbers.

As a final step, you can change the column types to numbers. 

 

 

let
    Source = Table1,
    #"Added Custom" = Table.AddColumn(Source, "Challenge Score", each Text.PositionOf("USGEO",[Challenge Grade])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Attainment Score", each Text.PositionOf("USGEO",[Attainment Grade])),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Custom1",{{"Challenge Score", Int64.Type}, {"Attainment Score", Int64.Type}})
in
    #"Changed Type"

 

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Thank you very much. I am afraid I am not yet savvy enough to understand exactly what that code is doing, but I'll give it a go. What does Text.PositionOf do exactly? And Int64.Type? I like to understand how the individual elements work so that I can then attempt to adapt and reuse them later on. Thanks again for your help!

Text.PositionOf returns the position (zero based) of the character in the second parameter, in the text in the first parameter, so

Text.PositionOf("USGEO","U") = 0

Text.PositionOf("USGEO","S") = 1, etcetera

 

Int64.Type is the result of changing the type of a column to whole number: select both column (Ctrl-click) and adjust the data type e.g. on the transform tab:

 

Change data type.png

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Lovely. Thank you very much for your help with this. 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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