Forum Discussion
Custom column based on several other columns
Given the data shown, what is the expected output? Also, can you post that data as text instead of a picture? Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490
- tgjones437 years ago
Helper IV
Thank you for the response. I have pasted the data below, with the column at the end showing the expected outcome.
ID ID 2 Name Value Custom Column 21770161 null Altitude 284 null 21770161 null Width 5 null 21770161 21779013 Program Core Fisheries Monitoring Program Survey 1 21770161 21779155 Program Core Fisheries Monitoring Program Survey 2 21770161 21779296 Program Water Framework Directive Programme Survey 3 21770161 21779013 Purpose Temporal Survey 1 21770161 21779155 Purpose Temporal Survey 2 21770161 21779296 Purpose Salmonid Survey 3 21770161 21779013 Repeat frequency 1 year Survey 1 21770161 21779155 Repeat frequency 1 year Survey 2 21770161 21779296 Repeat frequency 6 years Survey 3 21770161 21779013 Survey Strategy Quanitative Survey 1 21770161 21779155 Survey Strategy Semi-quantitative Survey 2 21770161 21779296 Survey Strategy Semi-quantitative Survey 3 21770161 21779013 Guideline Method PDC Electric Fishing Survey 1 21770161 21779155 Guideline Method PDC Electric Fishing Survey 2 21770161 21779296 Guideline Method Electric Fishing Survey 3 - Greg_Deckler7 years ago
Community Champion
So, could you just create a second query where you pull out all of the distinct values from ID2 and assign an Index to them. That way you should be able to easily relate that dimension table back to this fact table and have what you want, correct?
- v-lili6-msft7 years ago
Community Support
hi, tgjones43
What is your expected outcome Survey 1, Survey 2, Survey 3, Is it an index column for each ID and ID2?
You may try this way as below:
Step1:
Duplicate the basic query, then group by it as below:
Step2:
Filter null value for ID2 and remove Count column
Step3:
Add an index column by ID
= Table.Group( #"Filtered Rows", {"ID"}, {{"Partition", each Table.AddIndexColumn(_, "Index",1,1), type table}})and expand the column
Step4:
Merge the tables and expand the column
Step5:
Change the index column to text and add a custom column as below
=if [#"Table1 (2).Index"] = null then null else "Survey" & [#"Table1 (2).Index"]
Result:
Here is pbix file, please try it.
Best Regards,
Lin
- tgjones437 years ago
Helper IV
Hi v-lili6-msft
Thank you, that looks great. But is there a way to do this without creating a second query? I would like to include the functions you have described after several steps and would prefer everything to be in one query.
- Ola_S7 years agoFrequent Visitor
...a minor tweek to Lin's excellent solution, to match the 'expected outcome' above.
Kind regards, Ola
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rZNRT8IwFIX/SrNnTFh1Qx4NiE8k6kx4IDw07AqN3TruWsz+vXdMjNal2WBPvevuOflu27NeBzycTMZhHAajILdK0fKgjDQ2BSr5/V2wGbU1rWRq9rRG7v+6nI7DWyqfUe9QZFTNNAJbyHIPKKFkS51Lo1HmO3buaXMJo2gAFz6N/7ishAFkC/qAT40fbC4RtkYe4eySgW8mi4Uu67N5g6zQKJQPvVvzN+FPcyJURrOlHoxXKEAY9o5wsJBvK9oKWQUCPTT9NA1UiyY+aUoPXGLxCBVLDNJR72rNixV0V6I+ZQ/gf10Cmbw5kNj41A3qpeqG+cnKFJTMgS3B7HVaX8h8xh4VvQ2U29Ozo5fmob/GoZmgxcGn5l0i+6upNbJ8kMjyYIjI8mCYyLozeVPoondr7hJZB6NT/Byafpo+kXXgOkfWAewZOgf1UvXFkXXor3HoEdnNFw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, #"ID 2" = _t, Name = _t, Value = _t]), #"Grouped and Add.SortNo" = Table.Group(Source, {"ID"}, {{"Count", each Table.AddIndexColumn(_, "Sort",1,1), type table}}), #"Expanded Count1" = Table.ExpandTableColumn(#"Grouped and Add.SortNo", "Count", {"ID 2", "Name", "Value", "Sort"}, {"ID 2", "Name", "Value", "Sort"}), Group = Table.Group(#"Expanded Count1", {"ID 2"}, {{"Count", each _, type table}}), #"Added Add.SurveyNo (index)" = Table.AddIndexColumn(Group, "Custom Column", 0, 1), #"Changed Type" = Table.TransformColumnTypes(#"Added Add.SurveyNo (index)",{{"Custom Column", type text}}), #"Added Prefix" = Table.TransformColumns(#"Changed Type", {{"Custom Column", each "Survey " & _, type text}}), #"Removed Columns" = Table.RemoveColumns(#"Added Prefix",{"ID 2"}), #"Replaced Value" = Table.ReplaceValue(#"Removed Columns","Survey 0",null,Replacer.ReplaceValue,{"Custom Column"}), #"Expanded Count" = Table.ExpandTableColumn(#"Replaced Value", "Count", {"ID", "ID 2", "Name", "Sort", "Value"}, {"ID", "ID 2", "Name", "Sort", "Value"}), #"Sorted Rows" = Table.Sort(#"Expanded Count",{{"ID", Order.Ascending}, {"Sort", Order.Ascending}}), #"Removed SortNo" = Table.RemoveColumns(#"Sorted Rows",{"Sort"}) in #"Removed SortNo"- v-lili6-msft7 years ago
Community Support
hi, Ola_S
Could you please tell me if your problem has been solved? If it is, could you please mark the helpful replies as Answered?
Best Regards,
Lin