Power BI:当源包含空值(处理缺失值)时,使折线图连续

时间:2018-07-18 12:59:42

标签: graph charts powerbi dax

该问题衍生出了我之前发布的一个问题; Custom x-axis values in Power BI

假设以下数据集:

enter image description here

关注第二和第三行。如何使下面相应图中的线连续并且不停在中间?

enter image description here

在Excel中,我过去通过在为图形生成数据的公式中应用NA()来解决此问题。也许有使用DAX的类似解决方案吗?

1 个答案:

答案 0 :(得分:0)

简短版本:


您应该做什么:

Unleash python,并按照此处的步骤操作后,插入此脚本(不用担心,我也在此处添加了一些详细信息):

import pandas as pd

# retrieve index column
index = dataset[['YearWeek_txt']]

# subset input data for further calculations
dataset2 = dataset[['1', '2']]

# handle missing values
dataset2 = dataset2.fillna(method='ffill')

然后您就可以像这样设置它:

enter image description here

为什么要这么做:

就内置选项而言,据我所知,这是您的选择:

1。。类别YearMonth和类别x轴不起作用

enter image description here

2。。类别YearMonth和连续的x轴不起作用

enter image description here

3。数字YearMonth和分类x轴不起作用

enter image description here

4。数字YearMonth和连续的x轴不起作用

enter image description here


详细信息,从内置方法失败的原因开始:


1。。类别年月和类别x轴

我使用了以下类似于表的屏幕截图的数据集:

YearWeek   1     2
201603     2.37  2.83
201606           2.55
201607     2.98 
201611     2.33  2.47
201615     2.14  2.97
201619     2.15  2.02
201623     2.33  2.8
201627     3.04  2.57
201631     2.95  2.98
201635     3.08  2.16
201639     2.50  2.42
201643     3.07  3.02
201647     2.19  2.37
201651     2.38  2.65
201703     2.50  3.02
201711     2.10  2
201715     2.76  3.04

不,我没有手动复制所有数据的麻烦。只是您的YearWeek系列。其余的是2到3之间的随机数。

然后我将数据设置为1和2的数字,并在Power Query Editor中将YearWeek aa文本键入:

enter image description here

这是原始设置,其中包含与您类似的表格和图表:

enter image description here

数据按YearWeek_txt降序排列:

enter image description here

x轴设置为Categorical

enter image description here

结论: 失败


2。。CategoryYearMonth和数字x轴

使用与上述相同的设置,您可以尝试将x轴类型更改为Continuous

enter image description here

但是,正如您所看到的,它可能会立即切换回“类别”,大概是因为YearWeek的类型是text

结论: 失败


3。数字YearMonth和分类x轴

我已经复制了原始设置,所以我得到了两个分类表和数值表,其中YearWeek的类型分别为textinteger

enter image description here

因此,数值YearMonth和分类x轴仍然可以为您提供:

enter image description here

结论: 失败


4。数字YearMonth和连续的x轴不起作用

但是 now ,使用与上述相同的设置,您可以将x轴类型更改为Continuous

enter image description here

您将得到以下结果:

enter image description here

结论: LOL


现在,Python:


Power Query Editor中,激活Categorical表,选择Transform > Run Python Script并将以下代码段插入到Run Python Script Editor中:

#'dataset'保存此脚本的输入数据

import pandas as pd

# retrieve index column
index = dataset[['YearWeek_txt']]

# subset input data for further calculations
dataset2 = dataset[['1', '2']]

# handle missing values
dataset2 = dataset2.fillna(method='ffill')

单击OK,然后单击Table旁的dataset2

enter image description here

您将获得此信息(确保列数据类型正确):

enter image description here

如您所见,不再缺少任何值。 dataset2 = dataset2.fillna(method='ffill')已将两列中的所有缺失值替换为先前的值。

单击“关闭并应用”以返回到桌面,并享受没有更多缺失值的表格和图表:

enter image description here

结论: Python很酷


尾注:

这里有很多细节,小数点,数据类型等可能会出错。让我知道您的工作情况如何,如果对您不利,我将再次进行查看。