Forum Discussion

victorbetancurt's avatar
victorbetancurt
Regular Visitor
7 years ago
Solved

Distinct Count with value in same table

Hi everyone.

Beforehand, sorry for my bad english.

I have a table like this ('Table1'):

CODESTATE
1015A
1016A
1017B
1015B
1016B
1017B
1015C
1015A
1017B


I want a solution as optimal as possible. (I'd rather calculate with a Power Query function, not DAX).

I want a function (or formula, whatever) that helps me to count every different state for each row with the same code.

My result should be this:

CODESTATERESULT
1015A3
1016A2
1017B1
1015B3
1016B2
1017B1
1015C3
1015A3
1017B1

I explain the result: For code 1015 is 3 because there are 3 unique values for 1015 (A,B,C). For code 1016 is 2 there are 2 unique values (A,B). For code 1017 is 1 because there are 1 unque value for 1017 (B).

  • Cmcmahan's avatar
    Cmcmahan
    7 years ago

    Another thing you may want to try then is instead of appending the two data tables, merge them side by side.

     

    So intead of your original table, you end up with something like this:

     

    CODEPREV_STATECUR_STATECHANGED
    1015AATRUE
    1016ABFALSE
    1017BBTRUE

     

    If whatever index you're using for each row stays the same between days, this may be a better way to store your data.  Be sure to check my previous reply for DAX code to solve your original problem

12 Replies

  • Cmcmahan's avatar
    Cmcmahan
    Resident Rockstar

    Though I'm not sure why you want this as a PowerQuery function instead of as a DAX, but it's possible.

     

    Go into the Query Editor, and at the far left of the Transform tab, you should see a Group By button.  Go through that wizard and Group By both Code and State.  Here's the entirety of the Power Query I used to create the results table as you have with the data snippet provided, with the bolded section being the important one:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwNFXSUXJUitUBc8yQOeZAjhOMY4rMMUPmYChzRuagmxYLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Code = _t, State = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Code", Int64.Type}, {"State", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Code", "State"}, {{"Result", each Table.RowCount(_), type number}})
    in
        #"Grouped Rows"

    You should be able to copy/paste the above into the advanced editor and play with it directly.

    • victorbetancurt's avatar
      victorbetancurt
      Regular Visitor

      Cmcmahan, I did it, but I lost the another columns, I want to keep them (in the example bottom I didn´t show them).

      PD: I suggested Power Query for better performance, (I think). If there is another way to achieve this without impacting performance, feel free to help me.

      • Cmcmahan's avatar
        Cmcmahan
        Resident Rockstar

        So the answer depends on what data is in those other columns.   If it's data that would make sense to aggregate (either through COUNT, SUM, AVERAGE, etc) you can add it in the Group By wizard, or with the advanced Power Query editor like so:

         #"Grouped Rows1" = Table.Group(#"Changed Type", {"Code", "State"}, {{"Result", each Table.RowCount(_), type number}, {"Highest Price", each List.Max([Price]), type text}})

         

        If you do not have data you want aggregated, you need to create a separate table and then do the Group By on that like before.  

         

        The problem with Power Query is that there isn't a way to dynamically copy another table.  When you press copy, it copies a snapshot of the current table, so if you upload new data, you have to copy the table over again, or set the new table to load from the same data source.

         

        I'm still not sure why you would strongly prefer Power Query instead of DAX for this.  Creating a custom summary table is incredibly easy in DAX, and I feel like if you have millions of rows of data, having to copy and then do Power Query transformations on the second table seems like a ton of extra processing.  

         

        I tried to search and see if there are performance differences for Power Query vs DAX, and can't find any information on that. And since DAX is built to create measures exactly like this, I would use that.