cancel
Showing results 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.

Employee

## How to Count the Lost Customers as per the his Latest status

Hi,

I have the below table

 Cust-ID Cust-Status Date 1 In 1/2/2020 2 Lost 1/5/2020 3 In 1/3/2020 1 Lost 1/4/2020 4 Lost 1/2/2020 2 In 1/6/2020

Want to count the "Cust-ID"s who have "Cust-Status" as both "In" ,"Lost" and Latest "Cust-Status" is "Lost".

Output of the above table should be 1 as only "Cust-ID" =1 is having the "Cust-Status" as Both "In and Lost" and  Latest "Cust-Status" is "Lost".

2 REPLIES 2
Resolver IV

Calculate Measure Version:

``````#LostCus =
VAR _CusWithNumStatus =
GROUPBY (
'Table',
'Table'[Cust-ID],
"@NumOfStatus", SUMX ( CURRENTGROUP (), 1 )
)
VAR _Customer =
_CusWithNumStatus,
"@Status",
CALCULATE (
MAX ( 'Table'[Cust-Status] ),
GENERATE (
KEEPFILTERS( VALUES ( 'Table'[Cust-ID] ) ),
CALCULATETABLE( TOPN ( 1, VALUES ( 'Table'[Date] ), 'Table'[Date] ) )
)
)
)
VAR _LostCus = FILTER ( _Customer, [@Status] = "Lost")
VAR _LostCus2Status = FILTER(_LostCus , [@NumOfStatus] = 2 )
RETURN
COUNTROWS( _LostCus2Status )``````
Community Champion

@Naresh91 , Power Query, DAX and Excel formula, all of them do the trick with ease.

Power Query solution,

``````let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLMAxJGBkYGugaGugZGSrE60UpGQCGf/OISZBlTsIwxhg5jsLghFh0mYBkTLDIIW1DNMlOKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Cust-ID" = _t, #"Cust-Status" = _t, Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Cust-ID", Int64.Type}, {"Cust-Status", type text}, {"Date", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Cust-ID"}, {{"Chk", each Table.Sort(_, {"Date", Order.Ascending})}}),
#"Lost Customer" = Table.TransformColumns(
#"Grouped Rows",
{
"Chk",
each let column = [#"Cust-Status"]
in if List.Last(column)="Lost" and List.ContainsAll(column, {"In","Lost"}) then "Lost Customer" else ""
}
)
in
#"Lost Customer"``````

DAX calculated column,

Excel array formula, our oldie but goodie, also my favorite,

``=IF((SUM(--(IF([Cust-ID]=[@[Cust-ID]],[Cust-Status])={"In","Lost"}))=2)*(INDEX([Cust-Status],MATCH(99999,[Date]/([Cust-ID]=[@[Cust-ID]])))="Lost"),"Lost Customer","")``

 Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension! DAX is simple, but NOT EASY!

Announcements

#### 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

#### Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

#### Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors