Forum Discussion
Single column containing two types for references with diff prefix but dynamic numbers changing
- 3 years ago
Hi rinaredelinghuy ,
The first thing I'd do would be to create new columns in Power Query with extra descriptors that I could later use to simplify writing measures. For example:
New columns in PQ:
referenceType = if Text.StartsWith([ReferenceNumber], "R") then "Picking" if Text.StartsWith([ReferenceNumber], "NB") then "Checking" else "Other"createDateOnly = Date.From([CreateDate])createDateHourOnly = Time.StartOfHour([CreateDate])This then makes writing explicit measures and creating visuals super-easy. Sums are just:
_confirmedPickingQty = CALCULATE( SUM(yourTable[ConfirmedQuantity]), yourTable[referenceType] = "Picking" )Or you could even just use a simple SUM(yourTable[ConfirmedQuantity]) measure, and use your new [referenceType], [createDateOnly], and [createDateHourOnly] dimensions in a visual to have the data split out correctly.
Pete
- 3 years ago
Ok, that actually simplifies things a bit, I think.
All we need is the [ReferenceNumber] value without the 'R:' or 'NB:' portion. You may already have this as [order no], [product], or [batch] but, in case you don't, you can get it into another new column something like this:
refOnly = Text.AfterDelimiter([ReferenceNumber], ":")Once you have that, you just need two simple measures:
_noofPicked = SUM(yourTable[ConfirmedQuantity]) // and _noofChecked = SUM(yourTable[CheckQuantity])You should now be able to acccurately visualise what you need using any/all of your existing and new dimensions, and these measures.
If you're struggling to get the output you want with what we've created so far, just post an example of exactly what you want it look like on here and I'll help you out.
Pete
- 3 years ago
Ok, give this a go. Just paste this into a blank query in Advanced Editor:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8gnQNTSwNDJV0lEKyEzOzsxLB7KCrMzMjS2BDEMLCwMzIG0AxrE6cPXGuNUb41LvnJEK0+DnBNRhYo6kA4KRdJhi02GE5CYjMI6NBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OrderNumber = _t, RefType = _t, ReferenceNumber = _t, IssueLineId = _t, ConfirmedQuantity = _t, CheckQuantity = _t]), chgTypes = Table.TransformColumnTypes(Source,{{"OrderNumber", type text}, {"RefType", type text}, {"ReferenceNumber", type text}, {"IssueLineId", Int64.Type}, {"ConfirmedQuantity", Int64.Type}, {"CheckQuantity", Int64.Type}}), groupIssueLineId = Table.Group(chgTypes, {"IssueLineId"}, {{"data", each _, type table [OrderNumber=nullable text, RefType=nullable text, ReferenceNumber=nullable text, IssueLineId=nullable number, ConfirmedQuantity=nullable number, CheckQuantity=nullable number]}, {"newConfirmedQty", each List.Sum([CheckQuantity]), type nullable number}}), expandDataColumn = Table.ExpandTableColumn(groupIssueLineId, "data", {"OrderNumber", "RefType", "ReferenceNumber", "ConfirmedQuantity", "CheckQuantity"}, {"OrderNumber", "RefType", "ReferenceNumber", "ConfirmedQuantity", "CheckQuantity"}) in expandDataColumnThis gives me the following output:
This works at the [IssueLineId] level but, if you had more dimensions, such as [product] etc. you'd want to add them into the groupRows step to ensure you were getting the [CheckQuantity] value over the correct subset of rows.
Pete
Hi,
I'm not really following I'm afraid. Would it be possible to show me a tiny example of what the source table looks like before, and then an example of how you want it to look after please?
It sounds like you're wanting to overwrite data in the table row, but this is rarely the solution to anything.
Pete
I want to create a new ConfirmedQty Column against the R:, displaying the sum of the CheckQty for the NB: linked via the IssueLineID.
In the example "AS IS" R:6739, IssueLineID 18803 & 18806 confirmed qty = zero. NB:6747 with IssueLineID 18803 is related to R:6739 with IssueLineID 18803, therefore I require the sum of CheckQuantity for NB:6747 with IssueLineID 18803 to be displayed as the ConfirmedQty for R:6739, IssueLineID
"AS IS"
TO BE
- BA_Pete3 years agoSuper User
Ok, give this a go. Just paste this into a blank query in Advanced Editor:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8gnQNTSwNDJV0lEKyEzOzsxLB7KCrMzMjS2BDEMLCwMzIG0AxrE6cPXGuNUb41LvnJEK0+DnBNRhYo6kA4KRdJhi02GE5CYjMI6NBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OrderNumber = _t, RefType = _t, ReferenceNumber = _t, IssueLineId = _t, ConfirmedQuantity = _t, CheckQuantity = _t]), chgTypes = Table.TransformColumnTypes(Source,{{"OrderNumber", type text}, {"RefType", type text}, {"ReferenceNumber", type text}, {"IssueLineId", Int64.Type}, {"ConfirmedQuantity", Int64.Type}, {"CheckQuantity", Int64.Type}}), groupIssueLineId = Table.Group(chgTypes, {"IssueLineId"}, {{"data", each _, type table [OrderNumber=nullable text, RefType=nullable text, ReferenceNumber=nullable text, IssueLineId=nullable number, ConfirmedQuantity=nullable number, CheckQuantity=nullable number]}, {"newConfirmedQty", each List.Sum([CheckQuantity]), type nullable number}}), expandDataColumn = Table.ExpandTableColumn(groupIssueLineId, "data", {"OrderNumber", "RefType", "ReferenceNumber", "ConfirmedQuantity", "CheckQuantity"}, {"OrderNumber", "RefType", "ReferenceNumber", "ConfirmedQuantity", "CheckQuantity"}) in expandDataColumnThis gives me the following output:
This works at the [IssueLineId] level but, if you had more dimensions, such as [product] etc. you'd want to add them into the groupRows step to ensure you were getting the [CheckQuantity] value over the correct subset of rows.
Pete
- rinaredelinghuy3 years agoHelper I
Hi Pete,
Thank you for sharing this solution, how do I get it now to be dynamic to run accross the IssueLineID column?
- BA_Pete3 years agoSuper User
You just need to swap out the 'Source' and 'chgTypes' steps with your own source details, and make sure the rest of the code refers to the correct column names as they appear in your actual source (which I'm hoping they should, as I've tried to use exactly the same names as your example).
This part is a bit more difficult for me to help with as I obviously don't have access to your actual sources, but let me know if you get stuck and I'll help you out where I can.
Pete