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
dccosta82
Frequent Visitor

Calculated column that checks whether a value in another column exists based on third value

Hi,

 

To simplify my issue:

 

Let's say I have a table with Route, Month and Location.

I want to be able to create calculated columns with specific month designations that check if that route is present for each month based on the Route field, kind of like pivoting the data without actually pivoting it.

 

Example of base:

 

Route, Month, Location


Lisbon-Porto, Jan, Lisbon
Lisbon-Porto, Feb, Lisbon
Lisbon-Porto, Mar, Lisbon
Lisbon-Porto, July, Lisbon

Porto-Lisbon, Jan, Porto


And turn it into:

 

Route, Month, Location, Jan?, Feb?, Mar?, Apr?, May?, June?, July?


Lisbon-Porto, Jan, Lisbon, YES, YES, YES, NO, NO, YES

Lisbon-Porto, Feb, Lisbon, YES, YES, YES, NO, NO, YES
Lisbon-Porto, Mar, Lisbon, YES, YES, YES, NO, NO, YES

Lisbon-Porto, July, Lisbon, YES, YES, YES, NO, NO, YES

Porto-Lisbon, Jan, Porto, YES, NO, NO, NO, NO, NO

 

Thanks in advance for all the help!

1 ACCEPTED SOLUTION

You want to add a column for each month on your dataset is that it?

 

In this case you have two options:

Power Query

 

  • Group your table by route and location
    • Select all rows in the option of aggregation

MFelix_0-1674827362499.png

  • Add a custom step that create a list based on month

 

Table.ToList (Table.SelectColumns ( [TableList], "Month"))

MFelix_1-1674827430160.png

 

  • Do merge with the Source step by location and route

MFelix_2-1674827680572.png

  • Expand the list from the table
  • Add a custom step with the following code:
= List.Accumulate({"Jan", "Feb", "Mar", "Apr", "May", "June", "July","Aug", "Sep", "Oct", "Nov", "Dec"}, 
                                #"Expanded Added Custom1", (state, current) => 
                                Table.AddColumn(state, current, each if List.Contains( [Added Custom1.Custom], current) then "YES" else "NO"))

 

Result below:

MFelix_3-1674827745157.png

 

Calculated Columns in DAX

 

For each month add the following column sintax:

July =
VAR routeselection = 'Table (2)'[Route]
VAR locationselection = 'Table (2)'[Location]
RETURN
    IF (
        "July"
            IN SELECTCOLUMNS (
                FILTER (
                    ALL ( 'Table (2)'[Route], 'Table (2)'[Month] ),
                    'Table (2)'[Route] = routeselection
                        && 'Table (2)'[Location] = locationselection
                ),
                "Months", 'Table (2)'[Month]
            ),
        "YES",
        "NO"
    )

 

You need to replace the name of the Month and the text after the IF

MFelix_4-1674828242798.png

See attach PBIX file.

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

4 REPLIES 4
MFelix
Super User
Super User

HI @dccosta82 ,

 

Create a table with the months then add the following measure:

Check Month = IF(CALCULATE(COUNTROWS(Routes), Routes[Month] in  VALUES(Months[Month]))> 0 , "YES", "NO")

 

MFelix_0-1674763623234.png

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Thank you for the quick reply Miguel!

I would however really like to do it in a calculated column within that same table.

 

Can I do it?

You want to add a column for each month on your dataset is that it?

 

In this case you have two options:

Power Query

 

  • Group your table by route and location
    • Select all rows in the option of aggregation

MFelix_0-1674827362499.png

  • Add a custom step that create a list based on month

 

Table.ToList (Table.SelectColumns ( [TableList], "Month"))

MFelix_1-1674827430160.png

 

  • Do merge with the Source step by location and route

MFelix_2-1674827680572.png

  • Expand the list from the table
  • Add a custom step with the following code:
= List.Accumulate({"Jan", "Feb", "Mar", "Apr", "May", "June", "July","Aug", "Sep", "Oct", "Nov", "Dec"}, 
                                #"Expanded Added Custom1", (state, current) => 
                                Table.AddColumn(state, current, each if List.Contains( [Added Custom1.Custom], current) then "YES" else "NO"))

 

Result below:

MFelix_3-1674827745157.png

 

Calculated Columns in DAX

 

For each month add the following column sintax:

July =
VAR routeselection = 'Table (2)'[Route]
VAR locationselection = 'Table (2)'[Location]
RETURN
    IF (
        "July"
            IN SELECTCOLUMNS (
                FILTER (
                    ALL ( 'Table (2)'[Route], 'Table (2)'[Month] ),
                    'Table (2)'[Route] = routeselection
                        && 'Table (2)'[Location] = locationselection
                ),
                "Months", 'Table (2)'[Month]
            ),
        "YES",
        "NO"
    )

 

You need to replace the name of the Month and the text after the IF

MFelix_4-1674828242798.png

See attach PBIX file.

 


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Thank you!

 

The second method worked.

The first I didn't try because it's a calculated table and I have no access to the table transformations you show on the printscreens.

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.