Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Get column values based on condition in another column

Hello,

 

I'm trying to get values in a column that meet a condition in another column. For example, say I have two columns in a table, and I want to get all the values in column2 for which column1 value = "XYZ". So far, I've been able to get only the first or last values in column2 using CALCULATE with FIRSTNONBLANK or LASTNONBLANK functions, however, when I try getting all of the values in column2 it gives a 'multiple values were supplied' error.

 

Thanks in advance.

  • smpa01's avatar
    smpa01
    4 years ago

    Anonymous  you can use following two measures 

     

    _earliestBeginDate =
    CALCULATE (
        MIN ( Table2[Begin_Date] ),
        TREATAS ( SUMMARIZE ( Table1, Table1[Values] ), Table2[Values] )
    )
    _latestEndDate =
    CALCULATE (
        MAX ( Table2[End_Date] ),
        TREATAS ( SUMMARIZE ( Table1, Table1[Values] ), Table2[Values] )
    )

     

     

     

16 Replies

  • Anonymous ,

    You can create a new column

    Column 3= if([Column1] = "XYZ", [Column2] , Blank() )

     

    or

    Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

    • Anonymous's avatar
      Anonymous
      Not applicable

      amitchandak 

      Thanks! That function worked for me.

      Is it possible to do the same with a measure instead of a calculated column? Or does a measure output always have to be one value instead of a list of multiple values? Looks like it's not working with a measure for me. 

      • Anonymous's avatar
        Anonymous
        Not applicable

        No, you can't do with a measure.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  Anonymous ,

    I created some data:

    Here are the steps you can follow:

    1. Create measure.

    Max_Measure = CALCULATE(SUM('Table'[Column2]),FILTER(ALL('Table'),'Table'[Column1]="XYZ"))

    2. Result:

     

    Best Regards,

    Liu Yang

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

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous 

      Thanks! However, I'm working with text data and this formula doesn't work in my case and I'm running into the same issue where it's not giving any output with the error message saying that multiple values were supplied. Can this same (or a similar) measure formula be applied for a case where both columns are text data?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  Anonymous ,

     

    Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

     

    Best Regards,

    Liu Yang

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Anonymous 

       

      Here is a more specific description of what I'm trying to do:

       

      I have two tables. Assume the first table has the following structure:

      Table1

      Filter_Column1Filter_Column2Filter_Column3Filter_Column4Values
      abc10aaa10001
      xyz50bbb10002
      abc100ccc50003
      abc50aaa10004

       

      Assume the second table has the following structure: 

      Table2

      ValuesBegin_DateEnd_Date
      112/15/202112/17/2021
      212/14/202112/19/2021
      312/16/202112/20/2021
      412/17/202112/18/2021

       

      My goal is to do the following:

      Whenever a filter is selected for either of the Filter_Columns (so, any time a filter changes), then do the following:

      • First, figure out a list of Values from Table1 that meets the filter selection criteria
      • Then, figure out the begin and end dates for the list of these Values from Table2

      For example:

      Say, there is a slicer for Filter_Column1 and a user sets Filter_Column1 to be "abc", then:

      • Query Table1 to get a list of values that meets this filter selection, which will be: 1,3 and 4
      • Then, get the earliest begin date and the latest end date in Table2 for this range of these values which will be 12/15/2021 (since it's the earliest date) and 12/20/2021 (since it's the latest date)

       

       

      Thanks in advance.

      • smpa01's avatar
        smpa01
        Icon for Community Champion rankCommunity Champion

        Anonymous  you can use following two measures 

         

        _earliestBeginDate =
        CALCULATE (
            MIN ( Table2[Begin_Date] ),
            TREATAS ( SUMMARIZE ( Table1, Table1[Values] ), Table2[Values] )
        )
        _latestEndDate =
        CALCULATE (
            MAX ( Table2[End_Date] ),
            TREATAS ( SUMMARIZE ( Table1, Table1[Values] ), Table2[Values] )
        )

         

         

         

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Alvin_Zang 

       

      Yes, so essentially the function first queries Table1 to get the list of values that meet the filter selection criteria, and then queries Table2 to get the earliest start date and latest end date for the list of those values (the end result is one start date and one end date). 

      • Alvin_Zang's avatar
        Alvin_Zang
        Frequent Visitor

        Clear. I got another simple solution.

        •  make a many to many relation like the snapshot below

           

        • write a measure

                 

        Earliest Begin Date = MIN( 'Table2'[Begin_Date] )
        Lastest End Date = MAX( 'Table2'[End_Date] )