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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
prashantg364
Helper II
Helper II

Column DAX query

I am having data as per the below table

ProductDateCategory
Alpha25-02-2023Service
Alpha17-02-2023Service
Alpha06-02-2023Purchase
Beta05-02-2023Purchase
Beta25-02-2023Service
Gama22-02-2023Service
Gama16-02-2023Service
Gama21-01-2023Purchase
Gama17-02-2023Service

I want to add one column in the last as shown below with the name "New Category"
Please suggest how to create this column.

ProductDateCategoryNew Category
Alpha25-02-2023ServiceService_2
Alpha17-02-2023ServiceService_1
Alpha06-02-2023PurchasePurchase
Beta05-02-2023PurchasePurchase
Beta25-02-2023ServiceService_1
Gama22-02-2023ServiceService_3
Gama16-02-2023ServiceService_1
Gama21-01-2023PurchasePurchase
Gama17-02-2023ServiceService_2
5 REPLIES 5
tamerj1
Super User
Super User

Hi @prashantg364 

please try

New category =
VAR CurrentDate = 'Table'[Date]
VAR T1 =
CALCULATETABLE (
'Table',
ALLEXCEPT ( 'Table', 'Table'[Product], 'Table'[Category] )
)
VAR Check =
COUNTROWS ( T1 ) > 1
VAR T2 =
ADDCOLUMNS (
T,
"NewCat",
[Category] & "_"
& IF ( Check, RANKX ( T1, [Date],, asc, DENSE ) )
)
RETURN
MAXX ( FILTER ( T2, [Date] = CurrentDate ), [NewCat] )

FreemanZ
Super User
Super User

hi @prashantg364 

you may also try to add a column like:

Column = 
VAR _table = 
FILTER(
    TableName, 
    TableName[Product]=EARLIER(TableName[Product])
        &&TableName[Category]=EARLIER(TableName[Category])
)
VAR _index = RANKX(_table, [Date],,ASC)
RETURN
IF(
    COUNTROWS( _table)=1,
    [Category],
    [Category]&"_"&_index
)

 

it worked like:

FreemanZ_0-1677736021943.png

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1677731237644.png

 

 

New Category CC =
SWITCH (
    TRUE (),
    Data[Category] = "Purchase", Data[Category],
    Data[Category] = "Service",
        Data[Category] & "_"
            & COUNTROWS (
                WINDOW (
                    1,
                    ABS,
                    0,
                    REL,
                    SUMMARIZE (
                        FILTER ( Data, Data[Category] = "Service" ),
                        Data[Product],
                        Data[Date]
                    ),
                    ORDERBY ( Data[Date], ASC ),
                    KEEP,
                    PARTITIONBY ( Data[Product] )
                )
            )
)

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Thanks

but i am getting this output with your query

ProductDateCategoryNew Category
Alpha25-02-2023ServiceService_1
Alpha17-02-2023ServiceService_1
Alpha06-02-2023PurchasePurchase
Beta05-02-2023PurchasePurchase
Beta25-02-2023ServiceService_1
Gama22-02-2023ServiceService_1
Gama16-02-2023ServiceService_1
Gama21-01-2023PurchasePurchase
Gama17-02-2023ServiceService_1

 

Output is not unique... 

ProductDateCategoryNew Category
Delta17-02-23PuchasePurchase
Delta05-02-23ServiceService_1
Delta06-02-23ServiceService_1
Delta16-02-23ServiceSercvice_3

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.