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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Nicks612
Helper I
Helper I

Seeking help with the DAX formula : Groupby and lookupvalue functions

Dear All,

 

I need help with the following issues:

 

SerialNumberOK / NOKDBKeyProblem DescriptionRemarks
23032406494NOK20230405082149I have these three columns in my data. I want to return these three columns concerning the minimum value of DBKey. (Refer To Table.)DBKey is a combination of date and test time in the below format - YYYY-MM-DD-HH-MM-SS, which is always unique.
23032406494NOK20230401082331
23032506584NOK20230331082009
23032006260OK20230330081531
23032106349OK20230329064408
23032106349OK20230329063959
23032506584OK20230325190045
23032406494NOK20230324185510
23032006260NOK20230321155209
23032006260OK20230321094402

 

I want to generate the following table which contains a unique serial number with a minimum value of their respective DBKey values. For Example, for the "23032406494" SerialNumber, there are three different entries of DBKey which are - "20230405082149", "20230401082331" and "20230324185510". Out of these DBKey values for the mentioned SerialNumber, this DBKey value "20230324185510" should come as this is the lowest value among the three DBKey values. 

 

Resulted Columns:
SerialNumberOK / NOKDBKey
23032006260OK20230321094402
23032106349OK20230329063959
23032406494NOK20230324185510
23032506584NOK20230325190045

 

I have implemented this logic using the GroupBy and merge option in the Power BI query editor. However, I need to do it using DAX formulas like table function and table manipulation functions - after that, I create a measure to count the number of serial numbers.

 

Kindly, provide the solutions and let me know if anyone needs further clarifications or doubts on the problem.

 

 

Regards,

Yash P

2 ACCEPTED SOLUTIONS

Hi, @Nicks612 

Try below measure

 

measure(OK) = 
 var a =ADDCOLUMNS(
    VALUES('Table'[SerialNumber]),
    "DBKey",
    MINX(
                 FILTER(ALL('Table'),
                       'Table'[SerialNumber]= EARLIER('Table'[SerialNumber])
                    ),
                 'Table'[DBKey]
            ),
    "OK/NOK",
    MINX(
        FILTER(
            ALL('Table'),
            'Table'[DBKey]=MINX(
                             FILTER(
                                ALL('Table'),
                                'Table'[SerialNumber]= EARLIEST('Table'[SerialNumber])
                            ),'Table'[DBKey])
        ),
        'Table'[OK / NOK])
)
RETURN
COUNTX(FILTER(a,[OK/NOK]="OK"),[OK/NOK])

 


For NOK measure Replace OK with "NOK" in 

COUNTX(FILTER(a,[OK/NOK]="NOK"),[OK/NOK]) Part

 

Best Regards
Dangar 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

@Dangar332, Thank you so much for the solution! The solution which you provided is working. 

View solution in original post

8 REPLIES 8
AntrikshSharma
Super User
Super User

@Nicks612 Why do you have NOK for: 

23032506584NOK20230325190045

 

Solution 1 = 
ADDCOLUMNS (
    ADDCOLUMNS ( 
        VALUES ( 'Table'[SerialNumber] ),
        "DBKey", CALCULATE ( MIN ( 'Table'[DBKey] ) )
    ),
    "OK/NOK", 
        CALCULATE ( 
            SELECTEDVALUE ( 'Table'[OK / NOK] ),
            TREATAS ( { [DBKey] }, 'Table'[DBKey] )
        )
)

 

Solution 2 = 
CALCULATETABLE (
    'Table',
    TREATAS ( 
        ADDCOLUMNS ( 
            VALUES ( 'Table'[SerialNumber] ),
            "DBKey", CALCULATE ( MIN ( 'Table'[DBKey] ) )
        ),
        'Solution 1'[SerialNumber],
        'Table'[DBKey]
    )
)

 

Thanks, @AntrikshSharma, for the prompt reply!!

Sorry, for the typo mistake. It is "OK" instead of "NOK". 

Dangar332
Super User
Super User

Hi, @Nicks612 

Try below formula for new table

 

New Table = 
 ADDCOLUMNS(
    VALUES('Table'[SerialNumber]),
    "DBKey",
    MINX(
                 FILTER(ALL('Table'),
                       'Table'[SerialNumber]= EARLIER('Table'[SerialNumber])
                    ),
                 'Table'[DBKey]
            ),
    "OK/NOK",
    MINX(
        FILTER(
            ALL('Table'),
            'Table'[DBKey]=MINX(
                             FILTER(
                                ALL('Table'),
                                'Table'[SerialNumber]= EARLIEST('Table'[SerialNumber])
                            ),'Table'[DBKey])
        ),
        'Table'[OK / NOK])
)

 

 

Dangar332_0-1718346295171.png



Best Regards
Dangar 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks, @Dangar332, for the prompt reply. I have tried this formula and it could be helpful. However, I have a query related to the same concerns that,

 

I want to count rows of "OK" and "NOK" serial numbers in the generated table using the measure as per your solution. For Example, one measure should contain a value of 3 for "OK" and 1 for "NOK". Along with your query, How do count the serial numbers from the virtual table?

Hi, @Nicks612 

Try below measure

New Table = 
 ADDCOLUMNS(
    VALUES('Table'[SerialNumber]),
    "DBKey",
    MINX(
                 FILTER(ALL('Table'),
                       'Table'[SerialNumber]= EARLIER('Table'[SerialNumber])
                    ),
                 'Table'[DBKey]
            ),
    "OK/NOK",
    MINX(
        FILTER(
            ALL('Table'),
            'Table'[DBKey]=MINX(
                             FILTER(
                                ALL('Table'),
                                'Table'[SerialNumber]= EARLIEST('Table'[SerialNumber])
                            ),'Table'[DBKey])
        ),
        'Table'[OK / NOK])
)

 

Dangar332_0-1718445115180.png

 

Best Regards
Dangar 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.



Thanks! @Dangar332, It is working correctly, but in my case, I have a time slicer from another table and if I create one more physical table then it is difficult to establish the relationship between the two tables.

Earlier, I was unaware of this scenario, therefore I didn't mention that. Sorry, for the inconvenience happened.

Can you please guide me to make a logic that gives me a total serial number of the "OK" and "NOK" values (two different measures for "OK"  and "NOK") by creating a virtual table in measure without creating a physical table? 

Hi, @Nicks612 

Try below measure

 

measure(OK) = 
 var a =ADDCOLUMNS(
    VALUES('Table'[SerialNumber]),
    "DBKey",
    MINX(
                 FILTER(ALL('Table'),
                       'Table'[SerialNumber]= EARLIER('Table'[SerialNumber])
                    ),
                 'Table'[DBKey]
            ),
    "OK/NOK",
    MINX(
        FILTER(
            ALL('Table'),
            'Table'[DBKey]=MINX(
                             FILTER(
                                ALL('Table'),
                                'Table'[SerialNumber]= EARLIEST('Table'[SerialNumber])
                            ),'Table'[DBKey])
        ),
        'Table'[OK / NOK])
)
RETURN
COUNTX(FILTER(a,[OK/NOK]="OK"),[OK/NOK])

 


For NOK measure Replace OK with "NOK" in 

COUNTX(FILTER(a,[OK/NOK]="NOK"),[OK/NOK]) Part

 

Best Regards
Dangar 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@Dangar332, Thank you so much for the solution! The solution which you provided is working. 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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