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

Count lines in History table and sum against current table

Hi,

 

I have 2 fact tables in my model. fact1 that has one line per uniqueid for a project and fact2 that has one line per phase change in each project so history of project. They look as below and are connected by an inactive relationship on unique project id (1 to many) as the active relationships are to all the Dimension tables shared between them.

 

I would like to count how many checks there are per unique project id in the fact2 history table and bring it back as a count in the fact 1 table as below.

 

At the moment I have created a summarised table that filters on the history table where the checking status is not blank then counts the rows and sums it against the projectuniqueids. I think link this table to fact1 table and lookup the value using related(). 

 

I'm wondering if there's a dax function that can do this all in one go so I don't have duplicate summarised tables in the model?

I tried creating the calculated table as a variable but then I can't work out how to access a column in a table variable in the dax

 

Thank you

 

fact1project

Projectuniqeidstartdateenddate

Total check counts

External check count

Internal check count

1  211
2  220
3     

 

Fatct2projecthistory

ProjecthistoryuniqiueidProjectuniqueidprojecttransitionidProjectcheckingstatus
A11externalcheck
B12internalcheck
C13 
D21externalcheck
E23 
F24externalcheck
1 ACCEPTED SOLUTION
123abc
Community Champion
Community Champion

To achieve this in Power BI using DAX, you can create a calculated column in the "fact1project" table that counts the number of checks from the "fact2projecthistory" table for each unique project ID. You can use the SUMX and FILTER functions to perform this calculation. Here's the DAX formula to create the calculated column:

```DAX
Total Check Counts =
VAR ProjectUniqueID = 'fact1project'[Projectuniqueid]
RETURN
COUNTROWS(
FILTER(
'fact2projecthistory',
'fact2projecthistory'[Projectuniqueid] = ProjectUniqueID
&& NOT ISBLANK('fact2projecthistory'[Projectcheckingstatus])
)
)
```

This formula does the following:

1. It first captures the current project's unique ID in the `ProjectUniqueID` variable.

2. Then, it uses the `FILTER` function to filter the "fact2projecthistory" table for rows where the project unique ID matches the current project (`'fact2projecthistory'[Projectuniqueid] = ProjectUniqueID`) and where the checking status is not blank (`NOT ISBLANK('fact2projecthistory'[Projectcheckingstatus])`).

3. Finally, it counts the rows returned by the `FILTER` function using `COUNTROWS`, which gives you the total check counts for the project.

After creating this calculated column in the "fact1project" table, you can use it in your visuals to display the total check counts for each project. This way, you won't need duplicate summarized tables, and the calculation will be performed on the fly based on your data model relationships.

View solution in original post

2 REPLIES 2
cran
Helper I
Helper I

Thank you @123abc exactly what I needed

123abc
Community Champion
Community Champion

To achieve this in Power BI using DAX, you can create a calculated column in the "fact1project" table that counts the number of checks from the "fact2projecthistory" table for each unique project ID. You can use the SUMX and FILTER functions to perform this calculation. Here's the DAX formula to create the calculated column:

```DAX
Total Check Counts =
VAR ProjectUniqueID = 'fact1project'[Projectuniqueid]
RETURN
COUNTROWS(
FILTER(
'fact2projecthistory',
'fact2projecthistory'[Projectuniqueid] = ProjectUniqueID
&& NOT ISBLANK('fact2projecthistory'[Projectcheckingstatus])
)
)
```

This formula does the following:

1. It first captures the current project's unique ID in the `ProjectUniqueID` variable.

2. Then, it uses the `FILTER` function to filter the "fact2projecthistory" table for rows where the project unique ID matches the current project (`'fact2projecthistory'[Projectuniqueid] = ProjectUniqueID`) and where the checking status is not blank (`NOT ISBLANK('fact2projecthistory'[Projectcheckingstatus])`).

3. Finally, it counts the rows returned by the `FILTER` function using `COUNTROWS`, which gives you the total check counts for the project.

After creating this calculated column in the "fact1project" table, you can use it in your visuals to display the total check counts for each project. This way, you won't need duplicate summarized tables, and the calculation will be performed on the fly based on your data model relationships.

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.