Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Analyzing Data with Power BI and Power Pivot for Excel (Alberto Ferrari, Marco Russo) (z-lib.org).pdf
Скачиваний:
11
Добавлен:
14.08.2022
Размер:
18.87 Mб
Скачать

RETURN

IF ( ISBLANK ( RelPeriod ), "Future", RelPeriod

The two columns (RelPeriodCode and RelPeriod) are shown in the Date table in Figure 4-36.

FIGURE 4-36 The last two columns are computed using the formulas described in the previous paragraphs.

Being calculated columns, they are recomputed at every refresh of the data model. In this way, they change the tags assigned to the dates. You do not need to update the report, as it will always show the last processed day as today, the day before as yesterday, and so on.

Using overlapping periods

The techniques you have seen in the previous sections work fine, but they all have one strong limitation: The time periods cannot overlap. In fact, because you store the attribute that defines the time period in a calculated column, there can only be one value assigned to the column.

There are, however, scenarios where this is not possible. Suppose, for example, that you put some product categories on sale in different periods of the year. It is totally possible that, in the same period, more than one product category is on sale. At the same time, the same category might be on sale during multiple different time periods. Thus, in such a scenario, you cannot store the period of sales in the Products table or in the Date table.

A scenario in which you have many rows (categories) that need to be in

relationships with many rows (dates) is known as a many-to-many model. Many- to-many models are not easy to manage, but they provide extremely useful analyses and are worth describing. You will find a much more complete discussion on many-to-many models in Chapter 8, “Many-to-many relationships.” In this section, we only want to show that when many-to-many relationships are involved, the code tends to be harder to write.

The Discounts configuration table from this example is shown in Figure 4-37.

FIGURE 4-37 Different sales periods for different categories are stored in the Discounts configuration table.

Looking at the Discounts configuration table, you can see that in the first week of January 2007 and 2008, there are multiple categories on sale (computers and audio). The same applies to the first two weeks of August (audio and cell phones). In such a scenario, you can no longer rely on relationships, and you need to write DAX code that takes the current filter from the sale period and merges it with the already existing filter in Sales. This is accomplished by the following formula:

Click here to view code image

SalesInPeriod := SUMX (

Discounts,

CALCULATE (

[Sales Amount], INTERSECT (

VALUES ( 'Date'[Date] ),

DATESBETWEEN ( 'Date'[Date], Discounts[D

), INTERSECT (

VALUES ( 'Product'[Category] ), CALCULATETABLE ( VALUES ( Discounts[Cate