Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more.
Get startedGrow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.
I have a dataset as below. I want to combine 4 columns (Col 1, Col 2, Col 3, Col 4) to one column with delimiter and only with UNINQUE values. Is this possible in M query / DAX?
If I do a normal merge in M I am unable to ignore duplicate values as in first row. We have two 123s. And I am unable to ignore delimiter if no values as in 2nd row I am getting :::123 since Col 1, 2, 4 is NULLL
Name | Location | Date | Code 1 | Code 2 | Code 3 | Code 4 | Result Column |
John | India | 01-Jan-24 | 123 | 456 | 123 | 123; 456 | |
Jace | UK | 02-Jan-24 | 123 | 123 |
Solved! Go to Solution.
To achieve this in Power Query (M Query), you can use the following steps:
1. Load your dataset into Power Query.
2. Add a custom column that concatenates the values of Col 1, Col 2, Col 3, and Col 4 into a list.
3. Remove duplicate values from the list.
4. Convert the list back to text with your desired delimiter.
5. Handle the case where all values are NULL.
Here's the M Query code to achieve this:
```m
let
Source = YourDataSourceHere, // Replace this with your actual data source
AddCustomColumn = Table.AddColumn(Source, "Combined", each List.RemoveNulls({[Col 1], [Col 2], [Col 3], [Col 4]})),
RemoveDuplicates = Table.TransformColumns(AddCustomColumn, {"Combined", each List.Distinct(_)}),
ConvertToList = Table.TransformColumns(RemoveDuplicates, {"Combined", each Text.Combine(List.Transform(_, Text.From)), type text}),
HandleEmptyValues = Table.TransformColumns(ConvertToList, {"Combined", each if _ = "" then null else _})
in
HandleEmptyValues
```
This code adds a new column "Combined" to your table where the values of Col 1, Col 2, Col 3, and Col 4 are combined into a single text value with unique values separated by your desired delimiter. It also handles the case where all values are NULL by converting the result to null.
After applying these transformations, your dataset should have a new column with the desired concatenated and unique values.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
Hi, @DataArch17
Thanks to HotChilli's thoughts, here's my detailed addition to this.
Start by changing your data type to Text type, then create a custom column and use the following M expression:
= Table.AddColumn(#"Changed Type", "Combined", each Text.Combine(List.Distinct({[Code 1], [Code 2], [Code 3], [Code 4]}), ";"))
How to Get Your Question Answered Quickly
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi, @DataArch17
Thanks to HotChilli's thoughts, here's my detailed addition to this.
Start by changing your data type to Text type, then create a custom column and use the following M expression:
= Table.AddColumn(#"Changed Type", "Combined", each Text.Combine(List.Distinct({[Code 1], [Code 2], [Code 3], [Code 4]}), ";"))
How to Get Your Question Answered Quickly
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
To achieve this in Power Query (M Query), you can use the following steps:
1. Load your dataset into Power Query.
2. Add a custom column that concatenates the values of Col 1, Col 2, Col 3, and Col 4 into a list.
3. Remove duplicate values from the list.
4. Convert the list back to text with your desired delimiter.
5. Handle the case where all values are NULL.
Here's the M Query code to achieve this:
```m
let
Source = YourDataSourceHere, // Replace this with your actual data source
AddCustomColumn = Table.AddColumn(Source, "Combined", each List.RemoveNulls({[Col 1], [Col 2], [Col 3], [Col 4]})),
RemoveDuplicates = Table.TransformColumns(AddCustomColumn, {"Combined", each List.Distinct(_)}),
ConvertToList = Table.TransformColumns(RemoveDuplicates, {"Combined", each Text.Combine(List.Transform(_, Text.From)), type text}),
HandleEmptyValues = Table.TransformColumns(ConvertToList, {"Combined", each if _ = "" then null else _})
in
HandleEmptyValues
```
This code adds a new column "Combined" to your table where the values of Col 1, Col 2, Col 3, and Col 4 are combined into a single text value with unique values separated by your desired delimiter. It also handles the case where all values are NULL by converting the result to null.
After applying these transformations, your dataset should have a new column with the desired concatenated and unique values.
Did I answer your question? Mark my post as a solution! Appreciate your Kudos !!
Create a list with list.distinct.
Filter the list for any values you don't want using List.Select.
Then use Text.Combine to get the final column.
Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.
Check out the June 2024 Power BI update to learn about new features.
User | Count |
---|---|
93 | |
86 | |
80 | |
69 | |
68 |
User | Count |
---|---|
226 | |
129 | |
119 | |
83 | |
77 |