Forum Discussion
Help with Query (Greater than)
- 4 years ago
NewStep= let fx=(t)=>let a=Text.From(t),b=List.PositionOf(Text.ToList(a&"a"),"",0,each List.Contains({"0".."9"},_)),c=Splitter.SplitTextByPositions({0,b})(a) in {Number.From(c{0}),Text.Start(c{1},1)} in Table.AddColumn(PreviousStepName,"Check",each let a=fx([Col A]),b=fx([Col B]),c=if a{0}<>b{0} then Value.Compare(a{0},b{0}) else Value.Compare(a{1},b{1}) in if a{0}=null and b{0}=null then "None" else if c=-1 then "Col B greater" else if c=0 then "Matches" else "Col A greater")
- 4 years ago
OK. It looks like my initial suggestion of padding with "0" was closer to correct. The difficulty is that the padding expands "A" and "0A" to be the same thing but you want these treated differently when compared to "0". I.e., "0" > "A" but "0" < "0A".
Try this with the digits and the letters treated separately:
let DigitsA = Number.FromText(Text.Select([Col A], {"0".."9"})) ?? -1, DigitsB = Number.FromText(Text.Select([Col B], {"0".."9"})) ?? -1, LettersA = Text.Remove([Col A], {"0".."9"}), LettersB = Text.Remove([Col B], {"0".."9"}), Result = if [Col A] = [Col B] then "Matches" else if DigitsA > DigitsB then "Col A greater" else if DigitsA = DigitsB and LettersA > LettersB then "Col A greater" else "Col B greater" in ResultNote: The ?? -1 syntax replaces nulls with -1.
Works well and looks simple. Understood the use the of the pad length.
However it fails where if either of the Col A or B is just simply '0' and comparing this zero with a alpha (or alphanumeric value), it results in the alpha/alphanumeric value being greater than '0'.
i.e. (row 24)
Col A = 0
Col B = B
Output (to the naked eye) should be Col A is greater, however its outputting Col B is greater.
Would adding additional else if in the 'Result' be a solution? If so, what condition can be added?
Thanks
Try using "z" instead of "0" to right pad the LettersA and LettersB variables.