Power Query Count if formula

%3CLINGO-SUB%20id%3D%22lingo-sub-1926931%22%20slang%3D%22en-US%22%3EPower%20Query%20Count%20if%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1926931%22%20slang%3D%22en-US%22%3E%3CP%3Ei%20!%3C%2FP%3E%3CP%3ENeed%20support%20with%20a%20count%20if%20-formula%20in%20PowerQuery.%20I%20know%20how%20to%20do%20it%20in%20excel%20but%20not%20in%20Powerquery.%3C%2FP%3E%3CP%3EI%20need%20to%20count%20the%20different%20combinations%20for%20name%26amp%3Bstage%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20list%20is%20a%20combination%20of%20name%26amp%3Bcity%26amp%3Bstage%2C%20example%3A%3C%2FP%3E%3CP%3EAdamMunichFab%3C%2FP%3E%3CP%3EAdamMadridFab%3C%2FP%3E%3CP%3EBetaMunichTest%3C%2FP%3E%3CP%3EBetaMadridFab%3C%2FP%3E%3CP%3EBetaUlmTest%3C%2FP%3E%3CP%3EBetaMunichTest%3C%2FP%3E%3CP%3EBestMadridTest%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20result%20should%20be%3A%3C%2FP%3E%3CP%3EAdamMunichFab%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%202%26nbsp%3B%20%26nbsp%3B(two%20different%20cities%20for%20Adam%20and%20Fab)%3C%2FP%3E%3CP%3EAdamMadridFab%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%202%3C%2FP%3E%3CP%3EBetaMunichTest%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%201%3C%2FP%3E%3CP%3EBetaMadridFab%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%201%3C%2FP%3E%3CP%3EBetaUlmTest%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B3%26nbsp%3B%26nbsp%3B%26nbsp%3B(three%20different%20cities%20for%20Beta%20and%20Test)%3C%2FP%3E%3CP%3EBetaMunichTest%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%203%3C%2FP%3E%3CP%3EBetaMunichTest%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%203%3C%2FP%3E%3CP%3EBestMadridTest%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%203%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20this%20understandable%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20for%20your%20support!!%3C%2FP%3E%3CP%3Eskunki%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1926931%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1927210%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Count%20if%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1927210%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F879996%22%20target%3D%22_blank%22%3E%40skunki%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20we%20need%20splits%20texts%20on%20Name%2C%20City%20and%20Stage%2C%20do%20that%20on%20duplicated%20column.%20After%20that%20group%20City%20and%20Stage%20without%20aggregation%2C%20count%20number%20of%20rows%20for%20each%20generated%20table%20and%20expand%20it%20keeping%20finally%20only%20NameCityStage%20and%20Count.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGenerated%20script%20is%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Excel.CurrentWorkbook()%7B%5BName%3D%22Table3%22%5D%7D%5BContent%5D%2C%0A%20%20%20%20%23%22Duplicated%20Column%22%20%3D%20Table.DuplicateColumn(Source%2C%20%22W%22%2C%20%22WW%22)%2C%0A%20%20%20%20%23%22Split%20Column%20by%20Character%20Transition%22%20%3D%20Table.SplitColumn(%0A%20%20%20%20%20%20%20%20%23%22Duplicated%20Column%22%2C%0A%20%20%20%20%20%20%20%20%22W%22%2C%0A%20%20%20%20%20%20%20%20Splitter.SplitTextByCharacterTransition(%7B%22a%22..%22z%22%7D%2C%20%7B%22A%22..%22Z%22%7D)%2C%0A%20%20%20%20%20%20%20%20%7B%22Name%22%2C%20%22City%22%2C%20%22Stage%22%7D%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Grouped%20Rows%22%20%3D%20Table.Group(%0A%20%20%20%20%20%20%20%20%23%22Split%20Column%20by%20Character%20Transition%22%2C%0A%20%20%20%20%20%20%20%20%7B%22Name%22%2C%20%22Stage%22%7D%2C%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%22NameStage%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20each%20_%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20type%20table%20%5BName%3Dnullable%20text%2C%20City%3Dnullable%20text%2C%20Stage%3Dnullable%20text%2C%20WW%3Dtext%5D%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Count%20number%20of%20rows%20in%20each%20table%22%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20%23%22Grouped%20Rows%22%2C%0A%20%20%20%20%20%20%20%20%22Count%22%2C%0A%20%20%20%20%20%20%20%20each%20Table.RowCount(%5BNameStage%5D)%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Expanded%20NameStage%22%20%3D%20Table.ExpandTableColumn(%0A%20%20%20%20%20%20%20%20%23%22Count%20number%20of%20rows%20in%20each%20table%22%2C%0A%20%20%20%20%20%20%20%20%22NameStage%22%2C%20%7B%22WW%22%7D%2C%20%7B%22WW%22%7D%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Removed%20Other%20Columns%22%20%3D%20Table.SelectColumns(%23%22Expanded%20NameStage%22%2C%7B%22WW%22%2C%20%22Count%22%7D)%0Ain%0A%20%20%20%20%23%22Removed%20Other%20Columns%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Ewhich%20gives%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20341px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F235762i7383CF66E6CF77E8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1930204%22%20slang%3D%22en-US%22%3ERe%3A%20Power%20Query%20Count%20if%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1930204%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks%20so%20much%20for%20you%20support%20and%20quick%20answer.%3C%2FP%3E%3CP%3EI'll%20try%20that%20out%2C%20unless%20it%20sounds%20complicated%20to%20me%20%2C%20I%20am%20a%20beginner%20in%20power%20query.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20I%20ask%20one%20additional%20question%3F%20Would%20this%20also%20work%2C%20if%20one%20line%20would%20be%20duplicated%3F%20like%20in%20my%20example%2C%20if%20there%20are%20two%20lines%20with%20same%20content%20(%20line%3A%20BetaMunichTest)%20would%20it%20count%20as%20one%20entry%20(desired)%20or%20two%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%20again%2C%3C%2FP%3E%3CP%3Eskunki%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

i !

Need support with a count if -formula in PowerQuery. I know how to do it in excel but not in Powerquery.

I need to count the different combinations for name&stage:

 

the list is a combination of name&city&stage, example:

AdamMunichFab

AdamMadridFab

BetaMunichTest

BetaMadridFab

BetaUlmTest

BetaMunichTest

BestMadridTest

 

the result should be:

AdamMunichFab        2   (two different cities for Adam and Fab)

AdamMadridFab        2

BetaMunichTest          1

BetaMadridFab            1

BetaUlmTest                 3   (three different cities for Beta and Test)

BetaMunichTest            3

BetaMunichTest          3

BestMadridTest            3

 

Is this understandable?

 

thanks for your support!!

skunki

3 Replies

@skunki 

First we need splits texts on Name, City and Stage, do that on duplicated column. After that group City and Stage without aggregation, count number of rows for each generated table and expand it keeping finally only NameCityStage and Count.

 

Generated script is

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Duplicated Column" = Table.DuplicateColumn(Source, "W", "WW"),
    #"Split Column by Character Transition" = Table.SplitColumn(
        #"Duplicated Column",
        "W",
        Splitter.SplitTextByCharacterTransition({"a".."z"}, {"A".."Z"}),
        {"Name", "City", "Stage"}
    ),
    #"Grouped Rows" = Table.Group(
        #"Split Column by Character Transition",
        {"Name", "Stage"},
        {
            {"NameStage",
             each _,
             type table [Name=nullable text, City=nullable text, Stage=nullable text, WW=text]}
        }
    ),
    #"Count number of rows in each table" = Table.AddColumn(
        #"Grouped Rows",
        "Count",
        each Table.RowCount([NameStage])
    ),
    #"Expanded NameStage" = Table.ExpandTableColumn(
        #"Count number of rows in each table",
        "NameStage", {"WW"}, {"WW"}
    ),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded NameStage",{"WW", "Count"})
in
    #"Removed Other Columns"

which gives

image.png

 

@Sergei Baklan 

Thanks so much for you support and quick answer.

I'll try that out, unless it sounds complicated to me , I am a beginner in power query.

 

Can I ask one additional question? Would this also work, if one line would be duplicated? like in my example, if there are two lines with same content ( line: BetaMunichTest) would it count as one entry (desired) or two?

 

thanks again,

skunki

@skunki 

If you mean like this

image.png

when by modifying Grouped Rows step as

    #"Grouped Rows" = Table.Group(
        #"Split Column by Character Transition",
        {"Name", "Stage"},
        {
            {"NameStage",
             each Table.Distinct(_, {"WW"}),  // INSTEAD OF each _,
             type table [Name=nullable text, City=nullable text, Stage=nullable text, WW=text]}
        }
    ),