sql-server - Power Bi:过滤包含字符串的 SQL Server 表
问题描述
我在 SQL Server 中有 2 个表:
包含国家名称的表 Country :
表协会名称和协会工作所在国家的名称:
在 Power Bi 中,我必须创建一个过滤器国家(美国、德国、法国、英国……)来过滤表社会:
例如,如果我在过滤器国家中输入“美国”,在我的矩阵中我将有社会 A 和社会 B。如果我在过滤器国家中输入“法国”,我将有社会 B 和社会 C。
(我的第一个想法是在 SQL Server 中添加一些二进制字段“IsInThisCountry”,然后使用这些字段作为过滤器)像这样:
CASE WHEN country LIKE '%US%' THEN 1 ELSE 0 END 'IsUS'
但问题是如果我有 50 个国家,我将不得不创建 50 个过滤器
解决方案
如果你有兼容 130 或更高版本的 SQL Server(使用 string_split),你可以在你的数据模型中尝试这样的事情来分割你的社团表中的分隔国家:
;with countries as (
select 'germany' as country
union all
select 'sweden'
),
socities as (
select 'A' as society, 'germany-sweden' as countries
union all
select 'B', 'sweden'
),
societyByCountry as (
select c.society, value as Country from socities c
cross apply string_split(c.countries, '-') s
)
select c.country, s.society from countries c
inner join societyByCountry s on s.Country = c.country
推荐阅读
- javascript - 如何从 iframe 获取 youtube 视频 html 元素?
- javascript - 内容 div 之间的选项卡,第一个 div 在加载时未激活
- c# - 发送右键单击到窗口
- sql - 将 UDT 数据从 Oracle 获取到 SQL Server 的 T-SQL 脚本
- git - 在不丢失工作更改的情况下使新的 Git 初始化存储库与 Origin 保持一致
- javascript - 是否可以在不使用 Leaflet 中的函数 addTo(map) 的情况下初始化事件?
- regex - 标准 Sql 中的大写转换,但有例外
- reactjs - 尝试更新状态时发出警告
- formal-verification - 我可以使用 CSP(通信顺序过程)验证哪个系统?
- git - Composer 使用标签或自定义分支从自定义 Git 存储库中提取包