Power BI中基于条件计算新建Retailer Margin列的实现咨询
核心实现逻辑
先校验Retailer Landed Price字段值,仅在字段非0、非空的场景下执行MRP - Retailer Landed Price的计算,否则直接返回空值,不同工具的具体实现如下:
Excel/ WPS电子表格实现
在目标列第一行输入公式后下拉填充即可:=IF(OR(B2=0,ISBLANK(B2)),NA(),A2-B2)
注:假设A列为MRP列,B列为Retailer Landed Price列,按需替换为实际列标即可,NA()会返回表格标准空值
Python Pandas实现
代码示例:
import pandas as pd import numpy as np df['Retailer Margin'] = np.where( df['Retailer Landed Price'].isna() | (df['Retailer Landed Price'] == 0), np.nan, df['MRP'] - df['Retailer Landed Price'] )
SQL实现
查询新增字段的语句示例,按需替换表名即可:
SELECT *, CASE WHEN `Retailer Landed Price` IS NULL OR `Retailer Landed Price` = 0 THEN NULL ELSE MRP - `Retailer Landed Price` END AS `Retailer Margin` FROM 你的业务表名
如果需要直接更新表结构新增字段,先执行字段新增语句再批量UPDATE即可。
Power BI DAX实现
新建计算列的DAX表达式:
Retailer Margin = IF( ISBLANK('你的表名'[Retailer Landed Price]) || '你的表名'[Retailer Landed Price] = 0, BLANK(), '你的表名'[MRP] - '你的表名'[Retailer Landed Price] )
替换为实际表名即可生效。
内容的提问来源于stack exchange,提问作者Abhishek Govilkar
相关产品推荐
相关产品推荐

