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
Hennadii
Helper IV
Helper IV

Filter table based on related table with inactive relation

Initial Data

A Data Model with relations and draft of a Report Page are attached.

Model.png

 

Page view.png

 

Target
When a Period selected in a slicer (from a List), the Created Actions and Closed Actions tables should be appropriately filtered:
Created Actions - shows created actions within selected Period;

Closed Actions - shows closed actions within selected Period.

 

As far I understand, there should be used USERELATIONSHIP within CALCULATE function. But I'm confused to write complete expression for each table.

 

Created Actions =
CALCULATE (
     ... 
     USERELATIONSHIP ( 'Actions'[Created On], 'Calendar'[Date] )
)

Closed Actions =
CALCULATE (
     ... 
     USERELATIONSHIP ( 'Actions'[Closed On], 'Calendar'[Date] )
)

 


Please, help me to create and filter Created Actions and Closed Actions tables.

Example

Data

Periods

ID Period Name Start Date End Date
1 Period 1 01/01/2020 01/10/2020
2 Period 2 01/11/2020 01/20/2020

 

Dates and Periods

Date Period
01/01/2020 Period 1
.... Period 1
01/10/2020 Period 1
01/11/2020 Period 2
... Period 2
01/20/2020 Period 2

 

Actions

ID Description Created On Closed On
1 Action 1 01/01/2020 01/25/2020
2 Action 2 01/10/2020 01/11/2020
3 Action 3 01/15/2020 01/20/2020
4 Action 4 01/16/2020  

 

Expected Result

Case #1 - selected Period 1 (01/01 - 01/10).
A content of the two tables is a following:

Created Actions:

ID Action
1 Action 1 (01/01 - 01/25)
2 Action 2 (01/10 - 01/11)

 

Closed Actions:

(blank table)

 

Case #2 - selected Period 2 (01/11 - 01/20).

A content of the two tables is a following:

Created Actions Table:

ID Action
3 Action 3 (01/15 - 01/20)
4 Action 4 (01/16 - blank)

 

Closed Actions:

ID Action
2 Action 2 (01/10 - 01/11)
3 Action 3 (01/15 - 01/20)

 

1 ACCEPTED SOLUTION

Thank you all for assistance @amitchandak , @parry2k , @v-shex-msft 


I've got the solution!
I'll leave it here in case some one other faced the same situation:

 

I proceeded with solution to create intermediate table as following

Dates and Actions = 
VAR _calendar = 
    CALENDAR( MIN( 'Actions'[Created On]), MAX( 'Actions'[Closed On] ) )
RETURN
    SELECTCOLUMNS(
        FILTER (
            CROSSJOIN('Actions', _calendar),
            'Actions'[Created On] = [Date] ||
            'Actions'[Closed On] = [Date]
        ),
        "Action ID", 'Actions'[Action ID],
        "Date", [Date],
        "Event" , IF('Actions'[Created On] = [Date], "Created", "Closed" )
    )

The Dates and Actions table should has relations as in the post above.

In a Report Page needed to:

  1. Drop column Action ID from the Dates and Actions table;
  2. Make sure that it appears as Table;
  3. Expand Filter pane and drop Event from the Dates and Actions table into "Add data fields here" section;
  4. Make a copy of the table;
  5. Name one table as Created Actions and select Created in the Filter pane;
  6. Name another table as Closed Actions and select Closed in the Filter pane.

Filter by Event.png

 

 

After that, by selecting a Period in the slicer, only Created and Closed Actions within the period will be listed in two tables.

P.S. I'm sure, there should be a way to solve it using inactive relations without creating additional Dates and Actions table.

I'm sure about that, because if set active relation between Actions[Created On] and Calendar[Date] (see Data Model in the first post), the Actions table in a Report Page is filtered to only Created actions for the selected Period.

And it can be filtered to show only Closed actions if change active relation to Actions[Closed On] and Calendar[Date]

Unfortunatelly, there is no way to keep both relations as active ...

I'll give kudo and set "Accept as Solution" if someone propose how to do it through inactive relations.

View solution in original post

10 REPLIES 10
v-shex-msft
Community Support
Community Support

HI @Hennadii,

I'd like to suggest you create calculated tables to expand raw table records range and you can use them to link raw tables and calendars. It should help to do other advanced analysis.

Spread revenue across period based on start and end date, slice and dase this using different dates 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft , that could be a way to solution. But it does not give me expected result for now.


I created additional table as you suggested in the post you mentioned.

Dates and Actions = 
VAR _calendar = 
    CALENDAR( MIN( 'Actions'[Created On]), MAX( 'Actions'[Closed On] ) )
RETURN
    SELECTCOLUMNS(
        FILTER (
            CROSSJOIN('Actions', _calendar),
            'Actions'[Created On] <= [Date] &&
            'Actions'[Closed On] >= [Date]
        ),
        "Action ID", 'Actions'[ID],
        "Date", [Date]
    )

Here attached updated Model. One note to dates:

  • Start Date and End Date in Period table do not overlap each other --> 1-1 relation between Calendar and Dates and Periods
  • Created On and Closed On in Actions table can overlap each other --> 1-* relation between Calendar and Dates and Actions
    Model_v2.png

 

When I place Action ID from Dates and Actions into Report Page (in a Table view), and select a Period from a slicer, it shows all Actions ID which overlap Start / End Dates of the selected Period.

In that case if Action 1 has dates 01/01 - 01/30 and Period dates  01/10 - 01/20, the Action 1 will be shown in the results but it is not correct as I need to see only Actions which were Closed or Created in the selected Period.

 

Thank you all for assistance @amitchandak , @parry2k , @v-shex-msft 


I've got the solution!
I'll leave it here in case some one other faced the same situation:

 

I proceeded with solution to create intermediate table as following

Dates and Actions = 
VAR _calendar = 
    CALENDAR( MIN( 'Actions'[Created On]), MAX( 'Actions'[Closed On] ) )
RETURN
    SELECTCOLUMNS(
        FILTER (
            CROSSJOIN('Actions', _calendar),
            'Actions'[Created On] = [Date] ||
            'Actions'[Closed On] = [Date]
        ),
        "Action ID", 'Actions'[Action ID],
        "Date", [Date],
        "Event" , IF('Actions'[Created On] = [Date], "Created", "Closed" )
    )

The Dates and Actions table should has relations as in the post above.

In a Report Page needed to:

  1. Drop column Action ID from the Dates and Actions table;
  2. Make sure that it appears as Table;
  3. Expand Filter pane and drop Event from the Dates and Actions table into "Add data fields here" section;
  4. Make a copy of the table;
  5. Name one table as Created Actions and select Created in the Filter pane;
  6. Name another table as Closed Actions and select Closed in the Filter pane.

Filter by Event.png

 

 

After that, by selecting a Period in the slicer, only Created and Closed Actions within the period will be listed in two tables.

P.S. I'm sure, there should be a way to solve it using inactive relations without creating additional Dates and Actions table.

I'm sure about that, because if set active relation between Actions[Created On] and Calendar[Date] (see Data Model in the first post), the Actions table in a Report Page is filtered to only Created actions for the selected Period.

And it can be filtered to show only Closed actions if change active relation to Actions[Closed On] and Calendar[Date]

Unfortunatelly, there is no way to keep both relations as active ...

I'll give kudo and set "Accept as Solution" if someone propose how to do it through inactive relations.

amitchandak
Super User
Super User

@Hennadii , your approach seems right, the period is coming via date so should work.

 

Refer to my example of how to use userelation has been used with two dates

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

 

@amitchandak , you evaluated numbers in CALCULATE function, but I'd like to return columns or tables. I do not think I should write some comparison on dates, as it should come from relations. What should be written in Expression of CALCULATE function?

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

Sorry @amitchandak and @parry2k for confusing. I've updated my post with examples and expected results, so, I hope the issue I faced become more clear.

Looking forward for your advices.

@Hennadii let's assume you are counting rows of actions for created and closed on, you will have two measures like this

 

Create On Count = 
CALCULATE ( COUNTROWS( TableAction ), USERELATIONS( TableAction[CreatedOn], Calendar[Date] ) )

Create On Count = 
CALCULATE ( COUNTROWS( TableAction ), USERELATIONS( TableAction[ClosedOn], Calendar[Date] ) )

 

if it doesn't work, provide more context to your problem with sample data and share what you are trying to achieve,

 

I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

@parry2k , yes, the measures you provided work, but they count number of Actions.
I want to see these Actions as a list (column with values), not just a total number for selected Period.

@Hennadii what is your expected output? it is not clear. Read this post to get your answer quickly.

https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

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.