Forum Discussion
Calculated column to assign numeric code to dates
- 3 months ago
Hi all, thank you for your input but it turns out there was actually a very easy way to do this in Power Query. Steps below:
1. Use the group by function on the date column, setting the operation to "all rows", making a nested table for each date.
2. Add an index column (I used the custom starting point of 99001 and incremented at 1)
3. Expand the nested tables back out.
Each row now has a unique identifier based on its date!
Thank you!
Follow up question: is there a way to add a prefix to these numbers? For example, if I wanted all of them to start with 99.
that would be something like
Date ID =
"99" & RANKX(ALL('Table'[Date]), 'Table'[Date], , ASC, DENSE)
but once you add a text prefix, the column becomes a text data type rather than numeric.
- krdavies3 months agoHelper I
OK so the issue with this and with what pankajnamekar25 suggested is that I need to take this column and use it in an append query with another related table, so if it's just in the table on the Power BI side, I don't think there's a way for me to do this. Is there a related statement I can use in a calculated column in Power Query?
- jgeddes3 months agoSuper User
In Power Query you can use the Indexing function.
In this example I am adding the Index column and transforming it to add the prefix in one step.= let index = Table.AddIndexColumn(YourPreviousStep, "Index", 1, 1, Int64.Type) in Table.TransformColumns(index, {{"Index", each "99-" & Number.ToText(_), type text}})Full example code...
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtU1MNQ1MFWK1YFxjXSNzJC4xrqGJkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]), #"Changed column type" = Table.TransformColumnTypes(Source, {{"Date", type date}}), #"Added Index" = let index = Table.AddIndexColumn(#"Changed column type", "Index", 1, 1, Int64.Type) in Table.TransformColumns(index, {{"Index", each "99-" & Number.ToText(_), type text}}) in #"Added Index"- krdavies3 months agoHelper I
I'm having a hard time executing this. In your first example, I'm not sure what 'YourPreviousStep' should refer to, and when I tried the second, it introduced a new column of dates that were unrelated to my existing date column. I'm just barely an advanced beginner so I might need some extra assistance to complete this. Thank you in advance for your patience.