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