Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Help with Query (Greater than)

Hi All,   I have two columns with alphanumeric data type. I want to compare which column has the greater value. Sounds simple to the naked eye, but in Power BI query, I’m not getting the output I w...
  • wdx223_Daniel's avatar
    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")

     

  • AlexisOlson's avatar
    AlexisOlson
    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
        Result

    Note: The ?? -1 syntax replaces nulls with -1.