RANKX in measures - powerbi

I have a table with 2 columns: client and product_name.
I need to number the product_name for each client
client
product_name
rank
1
aaa
1
1
baa
2
1
cwe
3
2
te
1
3
aaa
1
3
cwq
2
I created a column
RANKX_column =
RANKX(
FILTER(Query1,Query1[client_id] = EARLIER(Query1[client_id])),
Query1[product_id],,ASC,Dense
)
but if I apply a filter, the rank is not recalculated.
I tried to rewrite this formula for measure, but it returns an error about the function EARLIER.

Try something like:
=
VAR ThisClientID =
MIN( Query1[client_id] )
RETURN
RANKX(
FILTER( ALL( Query1 ), Query1[client_id] = ThisClientID ),
CALCULATE( MIN( Query1[product_id] ) ),
,
ASC,
DENSE
)

It is better to do it using Power Query:
You need first to group by columns [Client] -- See picture:
Resulting Table:
Then create a custom column, Here is the M Code:
Table.AddIndexColumn([AllData],"Rank",1,1)
Resulting Table:
Then Combine (Union) the all tables using The M Code:
= Table.Combine(#"Added Custom"[Rank])
Then close & apply. Return to your Main menu:
If you check your data view , The table there seems:

Related

DAX Measure to count Rows if relation may not exist

I am looking for a DAX measure to solve the following problem:
Count the number of rows in the dimension table where the Fact table either has no rows or the score is 0.
Table A (Dimension Table)
ID
name
1
a
2
b
3
c
Table B (Fact Table)
ID
score
1
0
1
1
1
2
2
5
Expected Result
In this example, I would expect 2, as ID=1 has one row with score=0 and ID=3 as no corresponding row in the Fact Table.
I came up with this measure which gives me the number of rows that have no corresponding row in the fact table, but I am not able to integrate the first condition:
CALCULATE(COUNTROWS('Dimension'), FILTER ('Dimension', ISBLANK ( CALCULATE ( COUNT ('Fact'[id]) ) )))
Probably much more straightforward methods, but try this measure for now:
MyMeasure =
VAR MyTable =
ADDCOLUMNS(
Table_A,
"Not in Table_B", NOT (
Table_A[ID]
IN DISTINCT( Table_B[ID] )
),
"Zero Score in Table_B",
CALCULATE(
COUNTROWS( Table_B ),
Table_B[score] = 0
) > 0
)
RETURN
SUMX(
MyTable,
[Not in Table_B] + [Zero Score in Table_B]
)
You can also try this
CountID =
VAR ScoreZero =
COUNTROWS ( FILTER ( TableB, [score] = 0 ) )
VAR NonExistentIDs =
COUNTROWS ( EXCEPT ( DISTINCT ( TableA[ID] ), DISTINCT ( TableB[ID] ) ) )
RETURN
ScoreZero + NonExistentIDs
This also works, not sure it's a good idea to nest CALCULATE:
CALCULATE(COUNTROWS('Table_A'), FILTER ('Table_A', ISBLANK ( CALCULATE ( COUNT ('Table_B '[id]) ) ) || CALCULATE(COUNTAX(Filter('Table_B ','Table_B '[score]=0),'Table_B '[id])>=1)))

Need to Filter a Table Based on Top N from Another Table

I'm trying to return values for only the top 4 countries
Top 4 Fully Vaccinated 3 =
VAR _Countries = TOPN(4,VALUES(DimCountries[Country]),[Fully Vaccinated %],DESC)
RETURN
CALCULATE(
[Fully Vaccinated %],
FILTER (
ALL(DimCountries),DimCountries[Country] IN _Countries
)
)
However, I still get all rows with a value
Fully Vaccinated % =
DIVIDE(
SUMX(
GROUPBY(FactCovid19,DimCountries[CountryKey],"Current Fully Vacc",MAXX(CURRENTGROUP(),FactCovid19[FullyVaccinated])),
[Current Fully Vacc]
),
[MTotalPopulation]
)
If I try to hardcode the country names it works fine. I also, tried the TOPN expression as a new table and it does return the 4 countries.
Does anyone knows what am I missing?

How do I calculate how many distinct / unique products are available in multiple or all U.S. states?

I am trying to determine how many distinct products are available in multiple or all countries. (I am going to focus on 3 U.S. states, WA, OR and CA)
For an example, I have a table that looks like this:
Product
State
First
WA
First
CA
First
CA
Second
WA
Second
OR
Second
CA
Second
OR
Third
OR
Third
WA
Third
WA
Third
WA
Fourth
WA
Fourth
CA
Fourth
OR
Fourth
CA
From the table, I have 4 unique products, and 3 unique states. Using dax, I want to find the unique products that are available in all 3 states.
The output output should show 2, for product Second and Fourth since they have cover states WA, CA and OR.
I've attempted doing this with
dax = CALCULATE(DISTINCTCOUNT(table1[product],filter(table1,[state] IN {"WA","CA","OR"}))
but it returns a maximum number of distinct products for whichever state.
You can try this
Measure4 =
VAR _0 =
CONCATENATEX ( VALUES ( 'fct'[State] ), fct[State], ",", fct[State], DESC )
VAR _1 =
COUNTX ( VALUES ( 'fct'[State] ), fct[State] )
VAR _2 =
IF ( _1 > 2, _0, BLANK () )
RETURN
_2
Edit
You can create a following measure after the measure above to give you the desired result in a card
newMeasure =
COUNTX (
FILTER (
ADDCOLUMNS (
SUMMARIZE (
fct,
fct[Product],
"test", CONCATENATEX ( VALUES ( fct[State] ), fct[State], ",", fct[State], DESC )
),
"test2", [Measure4]
),
[test2] <> BLANK ()
),
[test2]
)
You can try with INTERSECT function (from your sample data measure return One, because product Fourth dont have 'OR'):
dax =
var __WA_product = CALCULATETABLE(VALUES(table1[product]), filter(table1,[state] = "WA"))
var __CA_product = CALCULATETABLE(VALUES(table1[product]), filter(table1,[state] = "CA"))
var __OR_product = CALCULATETABLE(VALUES(table1[product]), filter(table1,[state] = "OR"))
return
countrows( INTERSECT(INTERSECT(__WA_product, __CA_product ), __OR_product ))

Find which product is often ordered with her one in DAX

I have a powerbi report containing a table that looks like :
Product ID
Transaction ID
Qty
A
X
1
B
X
2
C
X
1
A
Y
2
B
Y
1
A
Z
1
C
Z
1
I would like to create a new table in dax, that would show for each unique occurence of the prodcut ID in the previous table, each product that has been bought at the same time (=in a transaction), and the sum of qty associated.
So for the example above, that would give a table like :
Product ID
Associated Product ID
Qty
Count of Transactions
A
B
3
2
A
C
2
2
B
A
3
2
B
C
1
1
C
A
2
2
C
B
2
1
I hope this is clear enough.
In SQL I could probably pull it off, but I have no idea how to this in DAX (using a DAX generated table)
Can somebody help me out ?
Thanks
this may be a little tricky and I don't know how it performs on a large dataset. I use GENERATE to create Main and AssociatedProduct (the first step create a corssjoin!), then we pack MainProd to variable to generate virtualtable with all transaction id (__Trans). Then we can easly compare [AssociatedProd] with source ProductId and Transaction with our __Trans. In the end, I remove SelfRelation (Product A vs Product A).
AssocAgg =
FILTER (
GENERATE (
SELECTCOLUMNS (
VALUES ( Associated[Product ID] ),
"MainProd", Associated[Product ID]
),
VAR __Main = [MainProd]
VAR __Trans =
CALCULATETABLE (
VALUES ( Associated[Transaction ID] ),
Associated[Product ID] = __Main
)
RETURN
ADDCOLUMNS (
SELECTCOLUMNS (
VALUES ( Associated[Product ID] ),
"AssociatedProd", Associated[Product ID]
),
"Trans",
CALCULATE (
COUNTROWS ( VALUES ( Associated[Transaction ID] ) ),
FILTER (
Associated,
[AssociatedProd] = Associated[Product ID]
&& Associated[Transaction ID] IN __Trans
)
),
"qty",
CALCULATE (
SUM ( Associated[Qty] ),
FILTER (
Associated,
[AssociatedProd] = Associated[Product ID]
&& Associated[Transaction ID] IN __Trans
)
)
)
),
[AssociatedProd] <> [MainProd]
)

Intersection of Customer product purchase (powerBI)

I need help with producing a count of the intersections between customers and which items they have purchased. For example, if there are 5 products, a customer can purchase any single product or any combination of the 5. Customers can also re-purchase a product at any date - this is where my problem arises as an end user wants to be able to see the intersections for any selected date range.
I have managed to come up with a solution which includes the use of parameters but this is not ideal as the end user does not have access to change any parameters of the report.
I'm open to any solution that does not involve parameters, ideally a slicer with dates would be the best solution
The fields I have on the table are customer_ID, date_ID, and product
Example Data
customer_id date_id product
1 9/11/2018 A
1 10/11/2018 A
1 10/11/2018 B
1 11/11/2018 C
1 11/11/2018 A
2 9/11/2018 C
2 10/11/2018 D
2 11/11/2018 E
2 11/11/2018 A
3 10/11/2018 A
3 10/11/2018 B
3 11/11/2018 A
3 11/11/2018 B
3 11/11/2018 B
4 10/11/2018 A
4 11/11/2018 A
5 9/11/2018 A
5 10/11/2018 B
5 10/11/2018 E
5 10/11/2018 D
5 11/11/2018 C
5 11/11/2018 A
6 9/11/2018 A
6 10/11/2018 A
6 11/11/2018 A
Possible output with different slicer selections
Any help at all would be greatly appreciated
This is pretty tricky since I can't think of a way to use the values of a dynamically calculated table as a field in a visual. (You can create calculated tables, but those aren't responsive to slicers. You can also create dynamically calculated tables inside of a measure, but measures don't return tables, just single values.)
The only way I can think of to do this requires creating a table for every possible product combination. However, if you have N products, then this table has 2N rows and that blows up fast.
Here's a calculated table that will output all the combinations:
Table2 =
VAR N = DISTINCTCOUNT(Table1[product])
VAR Products = SUMMARIZE(Table1,
Table1[product],
"Rank",
RANKX(ALL(Table1),
Table1[product],
MAX(Table1[product]),
ASC,
Dense
)
)
VAR Bits = SELECTCOLUMNS(GENERATESERIES(1, N), "Bit", [Value])
VAR BinaryString =
ADDCOLUMNS(
GENERATESERIES(1, 2^N),
"Binary",
CONCATENATEX(
Bits,
MOD( TRUNC( [Value] / POWER(2, [Bit]-1) ), 2)
,,[Bit]
,DESC
)
)
RETURN
ADDCOLUMNS(
BinaryString,
"Combination",
CONCATENATEX(Products, IF(MID([Binary],[Rank],1) = "1", [product], ""), "")
)
Then add a calculated column to get the column delimited version:
Delimited =
VAR Length = LEN(Table2[Combination])
RETURN
CONCATENATEX(
GENERATESERIES(1,Length),
MID(Table2[Combination], [Value], 1),
","
)
If you put Delimited the Rows section on a matrix visual and the following measure in the Values section:
customers =
VAR Summary = SUMMARIZE(Table1,
Table1[customer_id],
"ProductList",
CONCATENATEX(VALUES(Table1[product]), Table1[product], ","))
RETURN SUMX(Summary, IF([ProductList] = MAX(Table2[Delimited]), 1, 0))
And filter out any 0 customer values, you should get something like this:
So yeah... not a great solution, especially when N gets big, but maybe better than nothing?
Edit:
In order to work for longer product names, let's use a delimiter in the Combination concatenation:
CONCATENATEX(Products, IF(MID([Binary],[Rank],1) = "1", [product], ""), ",")
(Note the "" to "," change at the end.)
And then rewrite the Delimited calculated column to remove excess commas.
Delimited =
VAR RemoveMultipleCommas =
SUBSTITUTE(
SUBSTITUTE(
SUBSTITUTE(
SUBSTITUTE(Table2[Combination], ",,", ","),
",,", ","),
",,", ","),
",,", ",")
VAR LeftComma = (LEFT(Table2[Combination]) = ",")
VAR RightComma = (RIGHT(Table2[Combination]) = ",")
RETURN
IF(RemoveMultipleCommas <> ",",
MID(RemoveMultipleCommas,
1 + LeftComma,
LEN(RemoveMultipleCommas) - RightComma - LeftComma
), "")
Finally, let's modify the customers measure a bit so it can subtotal.
customers =
VAR Summary = SUMMARIZE(Table1,
Table1[customer_id],
"ProductList",
CONCATENATEX(VALUES(Table1[product]), Table1[product], ","))
VAR CustomerCount = SUMX(Summary, IF([ProductList] = MAX(Table2[Delimited]), 1, 0))
VAR Total = IF(ISFILTERED(Table2[Delimited]), CustomerCount, COUNTROWS(Summary))
RETURN IF(Total = 0, BLANK(), Total)
The Total variable gives the total customer count for the total. Note that I've also set zeros to return as blank so that you don't need to filter out zeros (it will automatically hide those rows).
You can also try this measure to calculate the result.
[Count Of Customers] :=
VAR var_products_selection_count = DISTINCTCOUNT ( Sales[product] )
VAR var_customers = VALUES ( Sales[customer_id] )
VAR var_customers_products_count =
ADDCOLUMNS(
var_customers,
"products_count",
VAR var_products_count =
COUNTROWS (
FILTER (
CALCULATETABLE ( VALUES ( Sales[product] ) ),
CONTAINS (
Sales,
Sales[product],
Sales[product]
)
)
)
RETURN var_products_count
)
RETURN
COUNTROWS (
FILTER (
var_customers_products_count,
[products_count] = var_products_selection_count
)
)
I think I've found a better solution/workaround that doesn't require precomputing all possible combinations. The key is to use a rank/index as a base column and then built off of that.
Since the customer_id is already nicely indexed starting from 1 with no gaps, in this case, I will use that, but if it weren't, then you'd want to create an index column to use instead. Note that there cannot be more distinct product combinations within a given filter context than there are customers since each customer only has a single combination.
For each index/rank we want to find the product combination that is associated with it and the number of customers for that combination.
ProductCombo =
VAR PerCustomer =
SUMMARIZE (
ALLSELECTED ( Table1 ),
Table1[customer_id],
"ProductList",
CONCATENATEX ( VALUES ( Table1[product] ), Table1[product], "," )
)
VAR ProductSummary =
SUMMARIZE (
PerCustomer,
[ProductList],
"Customers",
DISTINCTCOUNT ( Table1[customer_id] )
)
VAR Ranked =
ADDCOLUMNS (
ProductSummary,
"Rank",
RANKX (
ProductSummary,
[Customers] + (1 - 1 / RANKX ( ProductSummary, [ProductList] ) )
)
)
VAR CurrID =
SELECTEDVALUE ( Table1[customer_id] )
RETURN
MAXX ( FILTER ( Ranked, [Rank] = CurrID ), [ProductList] )
What this does is first create a summary table that computes the product list for each customer.
Then you take that table and summarize over the distinct product lists and counting the number of customers that have each particular combination.
Then I add a ranking column to the previous table ordering first by the number of customers and tiebreaking using a dictionary order of the product list.
Finally, I extract the product list from this table where the rank matches the index/rank of the current row.
You could do a nearly identical measure for the customer count, but here's the measure I used that's a bit simpler and handles 0 values and the total:
Customers =
VAR PerCustomer =
SUMMARIZE (
ALLSELECTED ( Table1 ),
Table1[customer_id],
"ProductList",
CONCATENATEX ( VALUES ( Table1[product] ), Table1[product], "," )
)
VAR ProductCombo = [ProductCombo]
VAR CustomerCount =
SUMX ( PerCustomer, IF ( [ProductList] = ProductCombo, 1, 0 ) )
RETURN
IF (
ISFILTERED ( Table1[customer_id] ),
IF ( CustomerCount = 0, BLANK (), CustomerCount ),
DISTINCTCOUNT ( Table1[customer_id] )
)
The result looks like this