Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

How to loop through a list and use the items as filter values

Hello all

I am trying to create a calculated table that contains valid employees based on a selection of dates from a slicer. Unfortunately, I don't know how to iterate through the date list. I am using the Dates and Employees tables as shown below. The first table shows three selected dates as in a slicer. The third table shows the expected table containing all employees where the selected dates are between Validfrom and Validto.

Date Table:

forrefa_0-1686827580777.png


Employee Table:

forrefa_1-1686827596927.png


Expected calculated Table:

2023-06-15_10-12-43.png

I have tried something like this:

CalculatedTable =
VAR Dates =
    ALLSELECTED ( Dates[Date] )
RETURN
    CALCULATETABLE (
        Employee;
        Dates[Date] >= Employee[ValidFrom]
            && Dates[Date] <= Employee[Validto]
    )


Can you please help how to do this in DAX. Thanks a lot

1 ACCEPTED SOLUTION

No, I'm afraid not. If you wanted to list the employees you would need to create a measure like

Dummy Measure =
VAR DatesAndEmployees =
    GENERATE (
        ALLSELECTED ( 'Date'[Date] ),
        SELECTCOLUMNS (
            FILTER (
                Employees,
                Employees[Valid from] <= 'Date'[Date]
                    && (
                        ISBLANK ( Employees[Valid to] )
                            || Employees[Valid to] >= 'Date'[Date]
                    )
            ),
            Employees[Employee ID],
            Employees[Name]
        )
    )
VAR Result =
    IF (
        SELECTEDVALUE ( Employees[Employee ID] )
            IN CALCULATE (
                VALUES ( Employees[Employee ID] ),
                KEEPFILTERS ( DatesAndEmployees )
            ),
        1
    )
RETURN
    Result

and use that as a filter on the visual to only show when the value is 1.

View solution in original post

5 REPLIES 5
johnt75
Super User
Super User

Calculated tables are only calculated during data load or refresh, so they don't pay any attention to slicers. If you want something to react to slicers dynamically you will need to use a measure instead.

You can store a table in a variable inside a measure, and then use that as a filter for any calculations you need to perform.

Anonymous
Not applicable

Hi  john75,

 

Could you provide a sample of what that would look like?  Thanks!

 

 

Dummy Measure =
VAR DatesAndEmployees =
    GENERATE (
        ALLSELECTED ( 'Date'[Date] ),
        SELECTCOLUMNS (
            FILTER (
                Employees,
                Employees[Valid from] <= 'Date'[Date]
                    && (
                        ISBLANK ( Employees[Valid to] )
                            || Employees[Valid to] >= 'Date'[Date]
                    )
            ),
            Employees[Employee ID],
            Employees[Name]
        )
    )
VAR Result =
    CALCULATE ( DISTINCTCOUNT ( Employees[Employee ID] ), DatesAndEmployees )
RETURN
    Result

The first variable generates the table you wanted to build. Not sure what you wanted to use it for, but this example counts the number of employees who were valid on any of the given dates.

Anonymous
Not applicable

Hi @johnt75 
Thanks for your reply. 
Is it possible to return the filtered table "DatesAndEmployees" and use it as a table in a visual (table, matrix) in Power BI?

No, I'm afraid not. If you wanted to list the employees you would need to create a measure like

Dummy Measure =
VAR DatesAndEmployees =
    GENERATE (
        ALLSELECTED ( 'Date'[Date] ),
        SELECTCOLUMNS (
            FILTER (
                Employees,
                Employees[Valid from] <= 'Date'[Date]
                    && (
                        ISBLANK ( Employees[Valid to] )
                            || Employees[Valid to] >= 'Date'[Date]
                    )
            ),
            Employees[Employee ID],
            Employees[Name]
        )
    )
VAR Result =
    IF (
        SELECTEDVALUE ( Employees[Employee ID] )
            IN CALCULATE (
                VALUES ( Employees[Employee ID] ),
                KEEPFILTERS ( DatesAndEmployees )
            ),
        1
    )
RETURN
    Result

and use that as a filter on the visual to only show when the value is 1.

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.