Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
clausm73
Helper III
Helper III

Value of bundles

I have a problem trying to calculate the value of bundles - and only bundles.

Transactions

DateSKUAmountCustomerID
01-01-2020991540001
01-01-2020105001
01-02-20209900960001
01-02-2020114115001
01-02-2020105001
01-02-2020100001

 

Bundles

BundleIDItemSKUBundleName
19915Bundle 1
11050Bundle 1
29900Bundle 2
31141Bundle 3

 

My expected output should look like:

DateSKUAmountCustomerIDBundleIDItemSKUBundleNameBundle Item CountTranaction Item Count
01-01-202010500111050Bundle 122
01-02-202010500111050Bundle 121
01-01-202099154000119915Bundle 122
01-02-2020990096000129900Bundle 211
01-02-202011411500131141Bundle 311

 

In the end I will filter only the rows where "Bundle Item Count" and "Transaction Item Count" matches.

 

SQL Query to get result:

WITH transactions AS (
SELECT *
FROM (
VALUES
('2020-01-01', 9915, 4000, 1)
,('2020-01-01', 1050, 0, 1)
,('2020-02-01', 9900, 96000, 1)
,('2020-02-01', 1141, 1500, 1)
,('2020-02-01', 1050, 0, 1)
,('2020-02-01', 1000, 0, 1)
) AS t([Date], [SKU], [Amount], [CustomerID] )
), bundles as (
SELECT *
FROM (
VALUES
(1, 9915, 'Bundle 1')
,(1, 1050, 'Bundle 1')
,(2, 9900, 'Bundle 2')
,(3, 1141, 'Bundle 3')
) AS bundles([BundleID], [ItemSKU], [BundleName])
), result AS (
SELECT
*
,[Bundle Item Count] = (SELECT count(*) FROM bundles WHERE [BundleID] = b.[BundleID])
FROM transactions c, bundles b
WHERE 1=1
AND c.[SKU] = b.[ItemSKU]
)
SELECT *
,[Tranaction Item Count] = (SELECT count(*) FROM result WHERE [BundleID] = m.[BundleID] and [Date] = m.[Date])
FROM result m

So far I have been able to get the following DAX but its not quite sufficient:

EVALUATE
ADDCOLUMNS (
ADDCOLUMNS (
GENERATE (
transaction;
FILTER ( bundles; transactions[SKU] = bundles[ItemSKU] )
);
"Bundle Item Count"; CALCULATE (
COUNTA ( bundles[ItemSKU] );
ALLEXCEPT ( bundles; bundles[BundleID] )
)
);
"Transaction Item Count"; CALCULATE (
COUNTA ( transactions[SKU] );
ALLEXCEPT ( transactions; transactions[Date] )
)
)

Any help would be appreciated,
Claus

1 ACCEPTED SOLUTION

Hi @clausm73 ,

 

I've changed the file with a new column to filter if the qty of itens in blundle is the same as the qty in transaction by date/bundle.

 

I hope it helps.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

5 REPLIES 5
camargos88
Community Champion
Community Champion

Hi @clausm73 ,

 

I've created this file: Download PBIX 

 

If you consider it as a solution, please mark as a solution and kudos.

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Thanks for the effort - but my issue is not solved.

This row is displayed as Bundle 1 - and I want it NOT to be - because the Transaction Item Count <> Bundle Item Count

DateSKUAmountCustomerIDBundleIDItemSKUBundleNameBundle Item CountTranaction Item Count
01-02-202010500111050Bundle 121

 

That why I need to create the Transaction Item Count in the first place - which I cannot solve.

I have managed to do a "workaround".


Created a column [BundlePath] in both tables using concatenation of column [ItemSKU] for each bundle - or potential bundle.
This column can then be used as a key in a relationship - or the [ItemSKU] values can be counted using PATHLENGTH on the concatenated value.

 

These counts - one for in 'bundles'[BundlePath] and one for 'transactions'[BundlePath] can then be compared in a new column [IsBundle].

Calculated column 'bundles'[BundlePath]

BundlePath =
CONCATENATEX (
CALCULATETABLE ( 'bundles'; ALLEXCEPT ( 'bundles'; 'bundles'[BundleID] ) );
'bundles'[ItemSKU];
"|";
'bundles'[ItemSKU]
)

 Calculated columns 'transaction'[LookupBundleID], 'transactions'[BundlePath] and 'transactions'[IsBundle]:

LookupBundleID =
LOOKUPVALUE ( Bundles[BundleID]; Bundles[ItemSKU]; Transactions[ItemSKU] )
 BundlePath =
CONCATENATEX (
CALCULATETABLE (
Transactions;
/* This is the essential statement to make it work correctly */
ALLEXCEPT (
Transactions;
Transactions[CustomerID];
Transactions[Date];
Transactions[LookupBundleID]
)
);
Transactions[ItemSKU];
"|";
Transactions[ItemSKU]
)
 IsBundle =
VAR _LookupBundleID = Transactions[LookupBundleID]
VAR _BundleItemCount =
CALCULATE ( COUNTA ( 'Bundles'[ItemSKU] ); Bundles[BundleID] = _LookupBundleID )
VAR _TransactionItemCount =
PATHLENGTH ( Transactions[BundlePath] )
RETURN
_BundleItemCount = _TransactionItemCount

 This gives the following result - based on the pbix file from the suggested answer:

 
 

Hi @clausm73 ,

 

I've changed the file with a new column to filter if the qty of itens in blundle is the same as the qty in transaction by date/bundle.

 

I hope it helps.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Based on your solution file I came up with a way to build calculated columns with concatenated [ItemSKU] values to solve my problem - and then compare item counts using PATHLENGTH() on the concatenated values.

Download PBIX 

 

2020-04-23 18_12_36-clausm73 - Power BI Desktop.png

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors