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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.