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
ankitkalsara
Helper I
Helper I

How to get active child record from one to many relationship in DAX

Hi all,

 

I want to get the active child record in one to many relationship. Below is the relationship.

 

Projects (One)

Tasks (Many)

 

  • For each Projects, we need to know which Task is active as of today.
  • If there are no active records as of today, then get the next task record.

e.g.

  • Project 1 - "Task 2" is active (since Start Date and End Date falls between today i.e. 10th May 2022)
  • Project 2 - "Task 3" is active (since there is no task which is active as of today and thus we need to get next task)

 

1.png

 

Attaching here the PBIX file as well.

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

CNENFRNL_0-1652216234914.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

6 REPLIES 6
v-yalanwu-msft
Community Support
Community Support

Hi @ankitkalsara ;

Did amitchandak solve your problem? I think his scheme is correct, or you can also use the following measure

Active Name = 
VAR _count =
    CALCULATE (
        COUNT ( [Task Id] ),
        FILTER (
            ALLEXCEPT ( 'Tasks', 'Tasks'[Project Id] ),
            [Start Date] <= TODAY ()
                && [End Date] >= TODAY ()) )
RETURN
    IF (
        TODAY () <= MAX ( 'Tasks'[End Date] )
            && TODAY () >= MAX ( 'Tasks'[Start Date] ),
        MAX('Tasks'[Task Name]),
        IF (
            _count < 1&&
         MAX ( 'Tasks'[Start Date] )
                    IN SUMMARIZE (
                        TOPN (
                            1,
                            FILTER ( ALLEXCEPT ( 'Tasks', 'Tasks'[Project Id] ), [Start Date] > TODAY () ),
                            [Start Date], ASC
                        ),
                        [Start Date]),
                        MAX([Task Name])))

The result as follow:

vyalanwumsft_0-1652425467808.png


Best Regards,
Community Support Team _ Yalan Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

CNENFRNL
Community Champion
Community Champion

CNENFRNL_0-1652216234914.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Hi @CNENFRNL , thank you for the solution 🙂

ankitkalsara
Helper I
Helper I

Hi @amitchandak :

 

The above code is not working for large datasets, do you know any other way to get the results?

 

Appreciate your help on this

amitchandak
Super User
Super User

@ankitkalsara , Create a measure like this and use in visual or use visual level filter

 

countrows(filter(Task, Task[Start Date]<=Today() && Table[End date] >= Today() ) )

 

also check

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

 

 

Hi @amitchandak

 

Thank you for your reply.

 

The measure gives the number of rows which are active. However, I want to get the row details which is active as of today and if not then the next row.

I tried the below code and it seems to be working for now. I created calculated column.

 

2.png

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.