Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago

Getting first and second row from related table

I have a related table (1..*) like this:

 

IdName
1Foo
2Bar
3Blah

 

In my main table, i want to select the first and second "Name" columns, e.g, this would be my main table:

SomeFieldSomeOtherFieldRelated1NameRelated2Name
abcdefFooBar

 

Thanks!

6 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      The main table is 1, and the table I'm trying to fetch the first and second rows is the "Many".

       

      Table[ID] = 1 won't work, as that'll be the record where ID == 1. I want the "first" record, so i assume there will need to be some TOP/ORDERBY in place?

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks for helping with this. As i said below though in response to amitchandak , the "Id" column is identity, not the row number. I want the "first" and "second" rows, not the rows with "Id=1" and "Id=2"

       

      In other words, the table could look like this:

      Id FK Name
      1 100 Foo
      2 100 Bar
      3 101 Blah
      4 101 Paa


      So, for FK 100, i want  id's 1 & 2, but for FK 101, i want id's 3&4. So the "first" and "second" row, for each "FK". 

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi Anonymous ,

         

        As you explained, you can try to add an index column in Power Query. Then you can get the 'first/second' row.

         

        If you want to add an index column with the grouping, refer to

        Create Row Number for Each Group in Power BI using Power Query

         

         

         

        Best Regards,

        Stephen Tao

         

        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

    Hi Anonymous ,

     

    Try to use ALL function

    Related1Name = CALCULATE(MAX('Table'[Name]),FILTER(ALL('Table'),[Id]=1))
    Related2Name = CALCULATE(MAX('Table'[Name]),FILTER(ALL('Table'),[Id]=2))

     

     

    Best Regards,

    Stephen Tao

     

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