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

DISTINCCOUNT with FILTERs based on unrelated calendartable

Dear all,

as DAX beginner I'm currently struggling with my code and I would much appreciate, if somebody could help me to find the right way.

I have two unrelated tables: 

  • DIM_calendar  with a date-colum
  • FACT_Projects with a start-column and an end-column

All the date related columnes are formatted as date.

In my report I have a Time related slicer for different visuals, where I'm e. g. showing montly results etc.

No I would like to show in another visual only the number of ongoing projects at the end of the selected time period (last month), means all projects which were ongoing at the beginning of the selected period by slicer, but ending earlier than the last month should not be considered. As both tables are unrelated (as I cannot create relation to tart-column and end-column) I'm using the TREATAS-Function.

 

I tried to create the following measure:

Ongoing_Projects = 
CALCULATE (
    DISTINCTCOUNT( FACT_Project[Project_ID]),
    FILTER
    (FACT_Project[Project_end] >  TREATAS  VALUES ( DIM_calendar[Date] ),  FACT_Project[end])
        && FACT_Project[start] <  TREATAS  VALUES ( DIM_calender[Date] )FACT_Project[von]))
    )
) 

which don't work 😥 I think the reason is, that I didn't adress the start and the end of the slicer selection correctly, but no idea how to solve this issue.
Can anybody help me ? Thanks a lot in advance for all efforts

Bärbel

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

Try

Ongoing projects =
VAR MaxDate =
    MAX ( 'Dim_calendar'[Date] )
VAR Result =
    CALCULATE (
        COUNTROWS ( 'FACT_project' ),
        'FACT_project'[start date] <= MaxDate
            && (
                ISBLANK ( 'FACT_project'[End date] )
                    || 'FACT_project'[End date] > MaxDate
            )
    )
RETURN
    Result

This assumes that there is only 1 entry in the fact table for each project. If there might be multiple entries then you can replace the COUNTROWS with DISTINCTCOUNT( FACT_Project[Project_ID])

View solution in original post

4 REPLIES 4
Baerbel
Helper I
Helper I

WoW  😃 you made my day, thanks so much, it works 🤣

In any case you would have another second for me to understand - why it works without a relationship between 

A relationship with the date table would only help to capture events that happened on a specific date, or within a range of specified dates. Here you are wanting to check whether one date was before the given date range and one was after, so a relationship wouldn't help.

Thanks - its a helpfull info for me for similiar issues 

johnt75
Super User
Super User

Try

Ongoing projects =
VAR MaxDate =
    MAX ( 'Dim_calendar'[Date] )
VAR Result =
    CALCULATE (
        COUNTROWS ( 'FACT_project' ),
        'FACT_project'[start date] <= MaxDate
            && (
                ISBLANK ( 'FACT_project'[End date] )
                    || 'FACT_project'[End date] > MaxDate
            )
    )
RETURN
    Result

This assumes that there is only 1 entry in the fact table for each project. If there might be multiple entries then you can replace the COUNTROWS with DISTINCTCOUNT( FACT_Project[Project_ID])

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.

Top Solution Authors