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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
pooling
Frequent Visitor

New table based on values calculated by measures (distinct count of values in another table)

I have a table with shipments data:

CalendarConsignorShipment#ClientShippingTypeCarrier
24/05/2021 00:00Cons_A430174Clie_XLTLCarr_1
26/05/2021 00:00Cons_B432496Clie_YFTLCarr_1
26/05/2021 00:00Cons_C432499Clie_ZFTLCarr_2
27/05/2021 00:00Cons_A432732Clie_ZFTLCarr_3
27/05/2021 00:00Cons_B432736Clie_YLTLCarr_3
28/05/2021 00:00Cons_C434673Clie_XFTLCarr_3
28/05/2021 00:00Cons_A433475Clie_YFTLCarr_2
24/06/2021 00:00Cons_B456648Clie_XLTLCarr_1
24/06/2021 00:00Cons_C454803Clie_ZFTLCarr_1

 

Calendar column: contains dates for each shipment. We will be interested to see results aggregated by months (I created a separate calendar table with a column YEAR-MONTH).

Consignor column: the key column here. Based on performance of each consignor during the selected month (in slicer), we need to assign a status to it (formula will be right after Table 1).

Shipment# column: unique names of shipments.

Client column: to whom the shipment was sent.

Shipping Type column: can be either FTL or LTL.

Carrier column: who delivered the shipment.

 

As a result, we need to get 3 tables in the report:

 

TABLE 1:

ConsignorsShipments (count) Selected month (slicer)Previous monthClients (distinct count) Selected monthPrevious monthShippingTypes (distinct count) Selected monthPrevious monthCarriers (distinct count) Selected monthPrevious monthStatus Selected monthPrevious monthPoints Selected month
Cons_A100120312221UserJunior3
Cons_B300402020Super ProfiInactive8
Cons_C5025441222ProfiSuper Profi-1

 

 

 

Status for Selected month = 
IF([Selected month clients] = 0, "Inactive,",
    IF([Selected month clients] = 1, "Junior",
        IF([Selected month clients] <= 3, "User",
            IF([Selected month clients] > 3 && [Selected month shipping types] > 1 && [Selected month carriers] >1, "Super Profi",
                IF([Selected month clients] > 3, "Profi",
                BLANK()
                )
            )
        )
    )
)

 

 

 

Status for previous month shoulb be calculated similarly, only instead of [selected month] columns the formula should take [previous month] columns.

 

 

 

Points = [Selected month clients] - [Previous month clients] + [Selected month shipping types] - [Previous month shipping types] + [Selected month carriers] - [Previous month carriers]

 

 

 

TABLE 2: Statuses

StatusConsignors (distinct count) Selected monthPrevious month%Shipments (count) Selected monthPrevious month%
Inactive01-100%000%
Junior01-100%0120-100%
User10100%1000100%
Profi10100%500100%
Super Profi110%302520%
Total330%18014524%

 

TABLE 3: New Consignors

Consignors (distinct count) Selected monthPrevious month%Shipments (count) Selected monthPrevious month%
10100%300100%

 

Consignor is new if [Selected month clients] > 0 && [Previous month clients] = 0.

 

The report should have slicers:

  1. selected month
  2. status

 

I am looking for help - how to do it all in DAX?

 

So far, I created this report with limited functionality - the user is unable to select a month, because my formulas are based on logic "selected month = current month(today)", and "previous month = one month before(today)" which I did in Power Query. In more detail, I created 3 grouped tables from the original table with shipments data:

  • for Current month (each Date.IsInCurrentMonth([Calendar]))
  • for Previous month (each Date.IsInPreviousMonth([Calendar]))
  • for 2 months ago (
    • StartDate = Date.AddMonths(Date.StartOfMonth(DateTime.FixedLocalNow()),-2),
    • EndDate = Date.AddMonths(Date.EndOfMonth(DateTime.FixedLocalNow()),-2),
    • each [Calendar]>=StartDate and [Calendar]<=EndDate)

In each table I had:

  • a list of all Consignors
  • respective count for [Shipment#] column
  • respective distinct count for [Client], [ShippingType], [Carrier]

Then I merged these three tables in one (the key column is a Consignor list). And then I created measures in DAX to calculate the report tables.

But, as I mentioned earlier, this method is limited to currrent calendar month only, while my User wants to select any month in the Slicer.

 

Somehow I think it would work better if I did not create these separate tables in PowerQuery either, because the report is working soooo slow now... I need somehow to calculate everything only with DAX formulas, just having the original table with shipments data. But I struggle how to do it... Please I need a hint!

 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

- use a data model (keep your tables separate and join them as needed)

- join the transaction table to the calendar table based on the date, not the yearmonth

- use the date (or yearmonth) column fom the calendar table, not from the transaction table

- use measures with PARALLELPERIOD.  use 0 for current month, -1 for previous month etc.

- (optional) use SWITCH() instead of nested IF()

View solution in original post

1 REPLY 1
lbendlin
Super User
Super User

- use a data model (keep your tables separate and join them as needed)

- join the transaction table to the calendar table based on the date, not the yearmonth

- use the date (or yearmonth) column fom the calendar table, not from the transaction table

- use measures with PARALLELPERIOD.  use 0 for current month, -1 for previous month etc.

- (optional) use SWITCH() instead of nested IF()

Helpful resources

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