cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
pjr1221
Frequent Visitor

Add Column with DAX function

Hello All, 

 

How can I add column using DAX? I don't know what function to use in DAX. 

Here is sample table,I can't show my real table because it's too big.

test11.PNG

 

 

 

 

 

 

 

 

 

 

 

 

I want add <GAP> column.

The formla is in the same <ID>, add differences value compared with previous week value.

below table is what i looking for table.

test.PNG

 

It is too difficult for me.. i'm beginner 
Please help me.

 

 

 

 

 

1 ACCEPTED SOLUTION

@pjr1221 

Edited:

I took the last line away. That was causing the variant data type error. You haven't described what you need if the result is neither >20% above or <20% below. The code below returns blank in those cases. If that's no what you need update it accordingly.

 

GAP_v2 =
VAR _ValuePreviousWeek =
    CALCULATE (
        DISTINCT ( Table1[Value] );
        ALLEXCEPT ( Table1; Table1[ID] );
        Table1[WEEK]
            = EARLIER ( Table1[WEEK] ) - 1
    )
RETURN
    IF (NOT ISBLANK ( _ValuePreviousWeek );
        VAR _Perc =
            DIVIDE ( Table1[VALUE] - _ValuePreviousWeek; _ValuePreviousWeek )
        RETURN
            SWITCH (
                TRUE ();
                _Perc >= 120 / 100; "Increase";
                _Perc <= 80 / 100; "Decrease"
) )

 

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

GAP =
VAR _ValuePreviousWeek =
    CALCULATE (
        DISTINCT ( Table1[Value] );
        ALLEXCEPT ( Table1; Table1[ID] );
        Table1[WEEK] = EARLIER ( Table1[WEEK] ) - 1)
RETURN
    IF ( NOT ISBLANK ( _ValuePreviousWeek ); Table1[VALUE] - _ValuePreviousWeek )

AlB
Super User
Super User

Hi @pjr1221 

 

Try this for your new calculated column. Table1 is the name of the table you show

 

 

GAP =
VAR _ValuePreviousWeek =
    CALCULATE (
        DISTINCT ( Table1[Value] );
        ALLEXCEPT ( Table1; Table1[ID] );
        Table1[WEEK] = EARLIER ( Table1[WEEK] ) - 1
    )
RETURN
    IF ( NOT ISBLANK ( _ValuePreviousWeek ); Table1[VALUE] - _ValuePreviousWeek )

 

On a different note, please always show your sample data in text-tabular format in addition to (or instead of) the screen captures. That allows people trying to help to readily copy the data and run a quick test, plus it increases the likelihood of your question being answered. Just use 'Copy table' in Power BI and paste it here.

 

 

pjr1221
Frequent Visitor

@AlB Thank you so much, AIB. In addition, if the difference is increased by more than 20%, I would like to enter "Increase", if it decreases by more than 20%, I would like to enter "Decrease". but what should I do?

Thank you very much for your help.

@pjr1221 

Edited:

I took the last line away. That was causing the variant data type error. You haven't described what you need if the result is neither >20% above or <20% below. The code below returns blank in those cases. If that's no what you need update it accordingly.

 

GAP_v2 =
VAR _ValuePreviousWeek =
    CALCULATE (
        DISTINCT ( Table1[Value] );
        ALLEXCEPT ( Table1; Table1[ID] );
        Table1[WEEK]
            = EARLIER ( Table1[WEEK] ) - 1
    )
RETURN
    IF (NOT ISBLANK ( _ValuePreviousWeek );
        VAR _Perc =
            DIVIDE ( Table1[VALUE] - _ValuePreviousWeek; _ValuePreviousWeek )
        RETURN
            SWITCH (
                TRUE ();
                _Perc >= 120 / 100; "Increase";
                _Perc <= 80 / 100; "Decrease"
) )

 

pjr1221
Frequent Visitor

@AlB  If the result is neither >20% above or <20% below, I would like to enter "Null".

pjr1221
Frequent Visitor

Hi, @AlB  I've applied it to my data, it's not working.

 

error is :

Expressions that yield variant data-type cannot be used to define calculated columns.

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors