My question is the extension of my previous question. The previous question was related to countifs and sumifs in powerquery, in this there is static search where product "P1" is searched, but P1 contains more values. For example P1xxxxxx.
Below is the updated screenshot of my table.
Below is the code which was answered in previous question
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"shop", type text}, {"shelf", type text}, {"product", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"shop"}, {
{"data", each Table.AddColumn(_, "countifs", each if [product]="p1" then 1 else 0), type table },
{"sumifs", each Table.RowCount(Table.SelectRows(_, each [product] = "p1")),type number }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"shelf", "product", "countifs"}, {"shelf", "product", "countifs"})
in #"Expanded data"
In above code p1 is searched static, but I tried to add p1 show that while filter it can take values before and after p1.
Below is the code which I tried.
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"shop", type text}, {"shelf", type text}, {"product", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"shop"}, {
{"data", each Table.AddColumn(_, "countifs", each if [product]="*p1*" then 1 else 0), type table },
{"sumifs", each Table.RowCount(Table.SelectRows(_, each [product] = "*p1*")),type number }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"shelf", "product", "countifs"}, {"shelf", "product", "countifs"})
in #"Expanded data"
the above code gives output as 0.
Asterisk (*) is not a wildcard in M. Try Text.StartsWith([product], "p1")
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"shop", type text}, {"shelf", type text}, {"product", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"shop"}, {
{"data", each Table.AddColumn(_, "countifs", each if Text.StartsWith([product], "p1") then 1 else 0), type table },
{"sumifs", each Table.RowCount(Table.SelectRows(_, each Text.StartsWith([product], "p1"))),type number }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"shelf", "product", "countifs"}, {"shelf", "product", "countifs"})
in #"Expanded data"
sample data:
shop | shelf | product |
---|---|---|
a | a1 | p123213 |
a | a2 | p212323 |
a | a3 | p113412 |
a | a4 | p14343 |
b | b1 | p251234 |
b | b2 | p2r5324 |
b | b3 | p1e3434 |
c | c1 | p1r43 |
c | c2 | p134 |
c | c3 | p4werwer |
c | c4 | p32343 |
c | c5 | p1234 |