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
HenrikHulstrøm
Frequent Visitor

Measure for counting on column by max value of another column

Please help me with a measure in F_TEST_COVERAGE table, that counts number of Test_Case_Number by max(Test_Case_Web_Id) where Test_Case_Type=Manual.

With filter for Module_Name set to 24.2, count should be 3 as Test_Case_Id=2 is the current(Max Test_Case_web_Id=1001 for Test_Case_Number).

Tables are:

HenrikHulstrm_0-1723121517105.png

D_TEST_CASE

TEST_CASE_IDTEST_CASE_NUMBERTEST_CASE_WEB_IDTEST_CASE_TYPE
1STC00011000Manual
2STC00011001SemiAutomated
3STC00011002Automated
4STC00022000Manual
5STC00033000Manual
6STC00044000Manual

D_MODULE

MODULE_IDMODULE_NAME
124.2
225.1
325.2

F_TEST_COVERAGE

MODULE_IDTEST_CASE_ID
11
12
14
15
16
21
22
24
31
34
1 ACCEPTED SOLUTION

Henrik,

 

Okay I think I'm following now.

 

In order, you are:

  • Filtering by MODULE_ID
  • Grouping by TEST_CASE_NUMBER
  • Filtering for only max TEST_CASE_ID
  • Filtering TEST_CASE_TYPE = "Manual"
  • Counting number of records

I missed the grouping and also misunderstood the order of operations. Thanks for your patience. 😄

 

Try:

-- calculates max web ID for each test case number
VAR TestCases = 
ADDCOLUMNS (
    VALUES ( D_TEST_CASE[TEST_CASE_NUMBER] ),
    "Max Web ID", CALCULATE ( MAX ( D_TEST_CASE[TEST_CASE_WEB_ID] ) )
)
-- gets values of all web IDs that are type manual
VAR ManualCases =
CALCULATETABLE (
    VALUES ( D_TEST_CASE[TEST_CASE_WEB_ID] ),
    D_TEST_CASE[TEST_CASE_TYPE] = "Manual"
)
-- filters max web ID table for only manual case types
VAR FilteredCases =
FILTER (
    TestCases,
    [Max Web ID] IN ManualCases
)
-- counts number of records
VAR Result = COUNTROWS ( FilteredCases )

RETURN Result

 

I've tested this using your own tables (thanks for pasting them as text in your original post instead of as a screenshot, making it easier to copy/paste - much appreciated!). I get 3, 1, and 2 respectively for your three module names.

 

One important note is I would change the relationship between D_TEST_CASE and F_TEST_COVERAGE to bi-directional. You have test coverage notated as a fact table but it functions more like a bridge table for two tables with a many-to-many relationship. This video from SQLBI on modeling many-to-many relationships might be useful if you haven't already watched it before.

 

Hope this helps.


----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)

 

P.S. Need a more in-depth consultation for your Power BI data modeling or DAX issues? Feel free to hire me on Upwork or DM me directly on here! I would love to clear up your Power BI headaches.

 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

7 REPLIES 7
HenrikHulstrøm
Frequent Visitor

Hi Wilson - thanks for helping out.
For Module_Id=1, Test_Case_Id 1 and 2 are in scope for Test_Case_Number STC0001. Test_Case_Id 2 is the current because it has the highest Test_Case_Web_Id and thus the Test_Case_type is SemiAutomated for STC0001.

Henrik,

 

Why is scope only limited to Test_Case_Number STC0001? Also, why is Test_Case_Id 2 included? Your original post specifically mentions Test_Case_Type = Manual.




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Visualizing it with an example, the steps to find the count are:

HenrikHulstrm_0-1723452617373.png

Count of current Test_Case_Id (or Test_Case_Number) where Test_Case_Type is Manual, is then 3.

Henrik,

 

Okay I think I'm following now.

 

In order, you are:

  • Filtering by MODULE_ID
  • Grouping by TEST_CASE_NUMBER
  • Filtering for only max TEST_CASE_ID
  • Filtering TEST_CASE_TYPE = "Manual"
  • Counting number of records

I missed the grouping and also misunderstood the order of operations. Thanks for your patience. 😄

 

Try:

-- calculates max web ID for each test case number
VAR TestCases = 
ADDCOLUMNS (
    VALUES ( D_TEST_CASE[TEST_CASE_NUMBER] ),
    "Max Web ID", CALCULATE ( MAX ( D_TEST_CASE[TEST_CASE_WEB_ID] ) )
)
-- gets values of all web IDs that are type manual
VAR ManualCases =
CALCULATETABLE (
    VALUES ( D_TEST_CASE[TEST_CASE_WEB_ID] ),
    D_TEST_CASE[TEST_CASE_TYPE] = "Manual"
)
-- filters max web ID table for only manual case types
VAR FilteredCases =
FILTER (
    TestCases,
    [Max Web ID] IN ManualCases
)
-- counts number of records
VAR Result = COUNTROWS ( FilteredCases )

RETURN Result

 

I've tested this using your own tables (thanks for pasting them as text in your original post instead of as a screenshot, making it easier to copy/paste - much appreciated!). I get 3, 1, and 2 respectively for your three module names.

 

One important note is I would change the relationship between D_TEST_CASE and F_TEST_COVERAGE to bi-directional. You have test coverage notated as a fact table but it functions more like a bridge table for two tables with a many-to-many relationship. This video from SQLBI on modeling many-to-many relationships might be useful if you haven't already watched it before.

 

Hope this helps.


----------------------------------
If this post helps, please consider accepting it as the solution to help other members find it quickly. Also, don't forget to hit that thumbs up and subscribe! (Oh, uh, wrong platform?)

 

P.S. Need a more in-depth consultation for your Power BI data modeling or DAX issues? Feel free to hire me on Upwork or DM me directly on here! I would love to clear up your Power BI headaches.

 




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Thanks a lot - spot on! 🤗

Perfect, thanks for the update. You're welcome, happy to help. 😄




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Wilson_
Super User
Super User

Hi Henrik,

 

I'm not following your logic. If your module name filter is 24.2, that means module = 1. For module = 1 in F_TEST_COVERAGE, I see test case ID of 1, 2, 4, 5, 6. Of those test case IDs, 1, 4, 5, 6 have a test case type of manual. This corresponds with test case web IDs of 1000, 2000, 3000, 4000. The max of those is 4000.

 

Where am I going wrong?




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





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.