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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Neko_Daisuki
Microsoft Employee
Microsoft Employee

Create new table after selecting slicer

I have the following two tables.
#Product

Product CategoryProduct CodeProduct Name
a brushp700brush k
b chanconp901shampoo y
a brushp902combo x
c maskp712hair mask v


#Purchase history

Purchase dateMember IDProduct CodeProduct CDNumber of purchasesIndex
20210210c10p700500011
20230210c10p901100012
20230216c10p902240033
20230616c10p901100024
20231016c10p901100035
20220410c28p901100016
20221216c28p902240037
20230816c28p901100028
20231121c28p901100039
20190410c01p7121000110
20221216c01p9022400311
20230816c01p9011000112
20230921c01p9011000213


I have two slicers installed.
From Product, "b chancon" in Product Category
From Purchase history, Number of purchases is "1"

Once the slicer selection is complete, I would like to use SUMMARIZECOLUMNS and ADDCOLUMNS to output the following results to a separate table.

Purchase dateMember IDProduct CodePurchase amountNumber of purchasesIndex
20210210c10p700500011
20190410c01p7121000110
20221216c01p9022400311


The output logic is that when you select "b chancon", the Product Code
is "p901".
Based on the results, we are looking for a way to identify customers who have purchased the product and display only their past purchase history.
Thanks for advance.

1 ACCEPTED SOLUTION

I apologize for the oversight. It seems that the 'Product Category' column has multiple values for the selected product, which is causing the error. To resolve this, we can modify the DAX code to handle this situation.

We can use the CALCULATETABLE function along with the VALUES function to filter the 'Product' table based on the selected 'Product Category' in the slicer. Here's an updated DAX code:

 

NewTable =
VAR SelectedProductCategory =
VALUES ( 'Product'[Product Category] )
VAR SelectedNumberOfPurchases =
VALUES ( 'Purchase history'[Number of purchases] )
RETURN
CALCULATETABLE (
SUMMARIZECOLUMNS (
'Purchase history'[Purchase date],
'Purchase history'[Member ID],
'Purchase history'[Product Code],
'Purchase history'[Number of purchases],
'Purchase history'[Index],
'Product'[Product Code],
'Product'[Product Category],
'Product'[Product Name],
'Purchase history'[Product CD],
'Purchase history'[Number of purchases],
'Purchase history'[Index],
'Purchase history'[Purchase amount]
),
'Product'[Product Category] IN SelectedProductCategory
)

 

This modification uses CALCULATETABLE to filter the 'Product' table based on the selected 'Product Category' from the slicer. The IN clause ensures that only rows with 'Product Category' values matching the selected category are included in the final result.

Please replace the column names with the actual column names from your dataset. Adjust the code accordingly based on your Power BI model and relationships. This modification should address the issue of multiple values in the 'Product Category' column.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

View solution in original post

4 REPLIES 4
Neko_Daisuki
Microsoft Employee
Microsoft Employee

Dear @123abc 

It wasn't the result I expected, but after making a few adjustments, I got the result I described.
Thank you for all your support.
I look forward to working with you.

123abc
Community Champion
Community Champion

To achieve the desired output, you can use the following DAX formula in Power BI. Assuming that you want to create a new table named "FilteredPurchaseHistory" based on the slicer selections:

 

FilteredPurchaseHistory =
VAR SelectedProductCategory = VALUES('Product'[Product Category])
VAR SelectedNumberOfPurchases = VALUES('Purchase history'[Number of purchases])
RETURN
ADDCOLUMNS(
SUMMARIZECOLUMNS(
'Purchase history'[Purchase date],
'Purchase history'[Member ID],
'Purchase history'[Product Code],
'Purchase history'[Number of purchases],
'Purchase history'[Index]
),
"ProductCategory", CALCULATE(MAX('Product'[Product Category]), 'Product'[Product Code] = 'Purchase history'[Product Code]),
"FilterCondition",
'Product'[Product Category] = SelectedProductCategory &&
'Purchase history'[Number of purchases] = SelectedNumberOfPurchases
)

 

This formula creates a new table by summarizing the relevant columns from the 'Purchase history' table and adds a couple of calculated columns:

  1. "ProductCategory": It retrieves the corresponding 'Product Category' for each 'Product Code' in the 'Purchase history' table.
  2. "FilterCondition": It checks if the selected product category and the number of purchases match the slicer selections.

The result is a table that includes only the rows where the product category matches the slicer selection, and the number of purchases is as selected. You can then use this new table for your analysis.

Note: Make sure to replace 'Product' and 'Purchase history' with the actual names of your tables in your Power BI model.

 
 
 

Dear 123abc,

Thank you for reaching out to us. We attempted to create a table using the DAX you provided.
However, we encountered an error due to multiple and duplicate items in "'Product'[Product Category]."

Product CategoryProduct CodeProduct Name
a brushp700brush k
b chanconp901shampoo y
a brushp902combo x
c maskp712hair mask v


The error message is as follows:

"Cannot determine a single value for column 'Product Category' in table 'Product'. This may occur when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."

Could you possibly provide a solution for this issue?

I apologize for the oversight. It seems that the 'Product Category' column has multiple values for the selected product, which is causing the error. To resolve this, we can modify the DAX code to handle this situation.

We can use the CALCULATETABLE function along with the VALUES function to filter the 'Product' table based on the selected 'Product Category' in the slicer. Here's an updated DAX code:

 

NewTable =
VAR SelectedProductCategory =
VALUES ( 'Product'[Product Category] )
VAR SelectedNumberOfPurchases =
VALUES ( 'Purchase history'[Number of purchases] )
RETURN
CALCULATETABLE (
SUMMARIZECOLUMNS (
'Purchase history'[Purchase date],
'Purchase history'[Member ID],
'Purchase history'[Product Code],
'Purchase history'[Number of purchases],
'Purchase history'[Index],
'Product'[Product Code],
'Product'[Product Category],
'Product'[Product Name],
'Purchase history'[Product CD],
'Purchase history'[Number of purchases],
'Purchase history'[Index],
'Purchase history'[Purchase amount]
),
'Product'[Product Category] IN SelectedProductCategory
)

 

This modification uses CALCULATETABLE to filter the 'Product' table based on the selected 'Product Category' from the slicer. The IN clause ensures that only rows with 'Product Category' values matching the selected category are included in the final result.

Please replace the column names with the actual column names from your dataset. Adjust the code accordingly based on your Power BI model and relationships. This modification should address the issue of multiple values in the 'Product Category' column.

 

If this post helps, then please consider Accepting it as the solution to help the other members find it more quickly.

 

In case there is still a problem, please feel free and explain your issue in detail, It will be my pleasure to assist you in any way I can.

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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