Forum Discussion

anesbbs's avatar
anesbbs
Frequent Visitor
3 years ago
Solved

Questions related with context

Hi, I'm new to Power BI.

I have dimCust table as follow:

| CustID | CustName | CustAge | CustLoc |

1A24X
2B22Y
3C32X
4D16X
5E54Z
6F32Z
7G27Y
8H43X
9I33Y
10J25Z
11K42Y
12L51X

 

And Sales table as follow:

| SalesID | CustID | Date | ProductID | Qty |

1401 April 202312
2302 February 202332
3413 February 202324
4514 January 202343
5213 April 2023612
6218 March 202333
7621 February 202325
8519 January 202336
9317 April 202343
10209 March 2023510
11622 March 202364
12717 February 2023410
13619 February 202339
14628 March 202327
15421 January 202312

 

The relationship is as follow:

 

 

I then created a measure table consists of several measures as follow:

  1. VisitQty = COUNT('Sales'[SalesID])
  2. EarliestVisit = MINX(Sales, [Date])
  3. LatestVisit = MAXX(Sales, [Date])
  4. PeriodDay = DATEDIFF([EarliestVisit], [LatestVisit], DAY)
  5. TotalPeriod = CALCULATE([PeriodDay], ALLSELECTED('dimCust'[CustID]))
  6. TotalVisit = CALCULATE(COUNT('Sales'[SalesID]), ALLSELECTED('dimCust'[CustID]))
  7. Visit/Period = DIVIDE([TotalVisit], [TotalPeriod])

Then in the report view, I chose Table Visualization and filled it with columns and measures:

  • CustID taken from dimCust table
  • Other columns are from the above measures

 

 

When I excluded CustName column as the above, the last 3 columns consist of fix total value for each column in all rows (red box), as I wanted it.

 

And as I filtered the CustLoc using slicer, the total values will also be adjusted and each row will still be filled with the fix total value as follows:

 

 

But when I included the CustName column, it filters the CustID column and the values in last 3 columns become only for the particular rows, as follows:

 

 

One more thing, I added other measure and included it in the table:

  1. VV = DIVIDE(([VisitQty] - [Visit/Period]*[PeriodDay])^2, [PeriodDay])

The result for each row is correct, but the total value become zero, as follows:

 

I have 3 questions:

1. How to show all CustID after add CustName column?

2. How to keep fix total value in each row based on slicer filter after add CustName column?

3. How to show correct total value for VV column?

 

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi anesbbs ,

     

    I suggest you to try code as below.

    TotalPeriod = CALCULATE([PeriodDay], ALLSELECTED('dimCust'[CustID]),REMOVEFILTERS(dimCust[CustName]))
    TotalVisit = CALCULATE(COUNT('Sales'[SalesID]),  ALLSELECTED('dimCust'[CustID]),REMOVEFILTERS(dimCust[CustName]))
    VV Total = IF(HASONEVALUE(dimCust[CustID]),[VV],SUMX(VALUES(dimCust[CustID]),[VV]) )

    Result is as below.

    Best Regards,

    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi anesbbs ,

     

    I suggest you to try code as below.

    TotalPeriod = CALCULATE([PeriodDay], ALLSELECTED('dimCust'[CustID]),REMOVEFILTERS(dimCust[CustName]))
    TotalVisit = CALCULATE(COUNT('Sales'[SalesID]),  ALLSELECTED('dimCust'[CustID]),REMOVEFILTERS(dimCust[CustName]))
    VV Total = IF(HASONEVALUE(dimCust[CustID]),[VV],SUMX(VALUES(dimCust[CustID]),[VV]) )

    Result is as below.

    Best Regards,

    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

     

     

    • anesbbs's avatar
      anesbbs
      Frequent Visitor

      Anonymous,

      There is another question that I need your help, still with the same case.

       

      I added another column to show current date in all rows.

      Tried using formula as per your suggestion, but not working: 

      CurrDate = CALCULATE(NOW(),  ALLSELECTED('dimCust'[CustID]),REMOVEFILTERS(dimCust[CustName]))

       

      Would you please help again on this one?

       

       

    • anesbbs's avatar
      anesbbs
      Frequent Visitor

      This is the outcome that I need:

      CustIDCustNameVisitQtyEarliestVisitLatestVisitPeriodDayTotal PeriodTotalVisitVisit/PeriodVV
      1A        
      2B309 March 202313 April 20233593150.1610.20
      3C202 February 202317 April 20237493150.1611.33
      4D321 January 202301 April 20237093150.1610.98
      5E214 January 202319 January 2023593150.1610.28
      6F419 February 202328 March 20233793150.1610.10
      7G117 February 202317 February 202309315  
      8H        
      9I        
      10J        
      11K        
      12L        
        1514 January 202317 April 202393   2.91