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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

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.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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