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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
egabor88
Frequent Visitor

Check if a pair of value is present in table1 and create a new column in table2

Hi All

 

I have tried to solve this problem for a while now but got stuck, and coulnd't find a previous post about it.

I have two tables. Table1 has list of customer ids and product ids, one customer id can occur multiple times with different or same product id. Table2 has a column with the unique customer ids.

I would like to create new columns for each product id in Table 2 that returns 1 if the pair of customer id and product id is present in table1.

 

Example:

Order NumberProduct Code
1a
1b
1c
1d
2c
2d
2a
2a
2a
3c
4d
4d

 

Table 2:

Order Numberabcd
11111
21011
30010
40001

 

I am using the below expression to create the new columns:

 

a = var _ordered = countx(filter('Table2', 'Table 2'[Order Number] = 'Table1'[Order Number] && 'Table1'[Product Code] = a), 'Table1'[Order Number])
return
if(not(isblank(_ordered)), 1, 0)
 
This creates the desired column, but i was wondering if it is possible to go through a dynamic list of product ids, and create all the required columns in one go, instead of doing it one by one. I have 69 unique product ids that can change in the future.
1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order Number", Int64.Type}, {"Product Code", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 1),
    #"Pivoted Column" = Table.Pivot(#"Added Custom", List.Distinct(#"Added Custom"[#"Product Code"]), "Product Code", "Custom", List.Min)
in
    #"Pivoted Column"

Hope this helps.

Ashish_Mathur_0-1714883947698.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order Number", Int64.Type}, {"Product Code", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each 1),
    #"Pivoted Column" = Table.Pivot(#"Added Custom", List.Distinct(#"Added Custom"[#"Product Code"]), "Product Code", "Custom", List.Min)
in
    #"Pivoted Column"

Hope this helps.

Ashish_Mathur_0-1714883947698.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi @Ashish_Mathur 

 

Thank you for your reply. This is exactly what i was looking for. Thank you very much.

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
barritown
Super User
Super User

Hi @egabor88,

One of the solutions:

1) You create two tables with unique products and orders. 

For example, like this:

Orders = DISTINCT ( 'Table'[Order Number] )
Products = DISTINCT ( 'Table'[Product Code] )

2) You create one-to-many connections between the newly created tables and your fact table.

3) Once done, you add a matrix visual and compose a simple measure like this one:

Measure = 
VAR counter = COUNTROWS ( 'Table' ) + 0
RETURN IF ( counter > 0, 1, 0 )

 And it works (see the attached PBIX file if needed):

barritown_0-1714815519718.png

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

Hi @barritown 

 

Thank you for your reply. Unfortuantely i need a table not just a visual. I would like to make further calculations with these new columns.

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.