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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Maya2988
Regular Visitor

Need help in creating Date Difference in Power BI

Hi All,

 

I amtrying to create a power bi reporte using SQL Server Direct Query mode. I have the below columns with some sample values

 

 

NumberCompanyStatusDate
1XXXGD29/09/2014 15:58:27
1XXXUC29/09/2014 12:43:39
2YYYGD30/09/2014 15:58:27
2YYYUD30/09/2014 12:43:39
3ZZZGD27/09/2014 15:58:27
3ZZZUC27/09/2014 12:43:39

 

Need to create table or matrix to show the below output. So need help in achiving the below output in Power BI

 

NumberCompanyStatus Hours Difference Between
  GDUC 
1XXX29/09/2014 15:58:2729/09/2014 12:43:393:15
2YYY30/09/2014 15:58:2730/09/2014 12:43:393:15
3ZZZ27/09/2014 15:58:2727/09/2014 12:43:393:15

 

Any help in achiving this Hours Difference Between calculation in Power or SQL.

 

I tried in SQL and got the below output

 

NumberCompanyGD DateUC DateHours Difference Between
1XXX29/09/2014 15:58:27NULL 
1XXXNULL29/09/2014 12:43:39 
2YYY30/09/2014 15:58:27NULL 
2YYYNULL30/09/2014 12:43:39 
3ZZZ27/09/2014 15:58:27NULL 
3ZZZNULL27/09/2014 12:43:39 

 

Thanks,

Maya

2 ACCEPTED SOLUTIONS
wini_R
Resolver III
Resolver III

Hi @Maya2988,

 

One way to achieve this is to create a new table based on the original one to get the expected structure and also set the right format for duration column:

wini_R_1-1715092539182.png

 

 

View solution in original post

Hey @Maya2988,

 

Just include that column in SUMMARIZE function:

table1 = 
ADDCOLUMNS(
    SUMMARIZE(
        tabA,
        tabA[Company],
        tabA[Number]
    )
    , "GD time", CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "GD")
    , "UC time", CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "UC")
    , "duration", CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "GD") - CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "UC")
)

View solution in original post

5 REPLIES 5
Maya2988
Regular Visitor

Hi @wini_R 

 

Thanks for the help. This helps me a lot and learnt a lot.Please tell me how to add Number also to this new table dax query (As i am new to power bi dont know how to do this)

Hey @Maya2988,

 

Just include that column in SUMMARIZE function:

table1 = 
ADDCOLUMNS(
    SUMMARIZE(
        tabA,
        tabA[Company],
        tabA[Number]
    )
    , "GD time", CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "GD")
    , "UC time", CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "UC")
    , "duration", CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "GD") - CALCULATE(SELECTEDVALUE(tabA[Date]), tabA[Status] = "UC")
)

Thanks @wini_R . It works perfectly. Last question regarding performance. Creating new table using DAX in power bi will create any performace issue in future. 

Yes, that might impact the performance depending on how big the original table is. It is always recommended (whenever possible) to do any tranformations in source system or as close to the source as possible (i.e. in SQL db).

wini_R
Resolver III
Resolver III

Hi @Maya2988,

 

One way to achieve this is to create a new table based on the original one to get the expected structure and also set the right format for duration column:

wini_R_1-1715092539182.png

 

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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