首页 > 解决方案 > Power BI:使用键盘重命名许多表和列

问题描述

有没有办法使用键盘重命名 Power BI 中的表和列。我有数百(或数千)个列和表,它们的名称需要比数据库中的名称更易于阅读。使用右键单击 | 重命名非常慢。切换到该列并按 F2 似乎不起作用。进入重命名模式的按键是什么?

或者...有没有办法在文本编辑器中打开一个 .pbix 文件,这样我就可以在那里完成工作?(当然,Microsoft 一定为文件选择了一些开放的、标准的、可移植的格式——比如 XML?;))我已经解压缩了文件,但 DataModel 文件似乎是二进制文件而不是存档文件。

标签: powerbipowerbi-desktop

解决方案


根据user12439754的回答...

(Power BI 中此任务的“易用性”非常糟糕。)

由于我使用的是 SQL Server,因此我能够编写一个完成大部分工作的脚本。

问题/未来增强:

  • 参数化架构(或搜索所有架构)。
  • 删除 #"Renamed Columns" 定义末尾的逗号。

用法:

  • 运行脚本。
  • 删除 #"Renamed Columns" 末尾的逗号。
  • 根据需要将列名移动到 #"Removed Columns"。
  • 将名称更改为您希望用户看到的名称。将结果(一次一个表)粘贴到高级编辑器中。
declare @q table (
    id int identity(1,1) not null,
    tbl varchar(128) not null,
    col varchar(128) not null
)

insert @q

select o.name as 'Table'
, c.name as 'Column'

from sys.sysobjects o
  inner join sys.syscolumns c on c.id = o.id
  inner join sys.schemas s on s.schema_id = o.uid

where s.name = 'dbo'

order by o.name
, c.colorder


declare @tbl varchar(128), @t varchar(128), @c varchar(128)
select @tbl = (select top 1 tbl from @q order by id)
declare @i int, @max int
set @i = 1
select @max = count(*) from @q

declare @out table(
    id int identity(1,1) not null, 
    a varchar(4000) not null
)


while @i <= @max
begin
    select @t = (select tbl from @q where id = @i)
    insert @out
    values ('let')
    , ('    Source = Sql.Database("FinancialDM", "FinancialDataMart"),')
    , ('    dbo_' + @t + ' = Source{[Schema="dbo",Item="' + @t + '"]}[Data],')
    , ('    #"Removed Columns" = Table.RemoveColumns(dbo_' + @t + ',{}),')
    , ('    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{')

    while @tbl = @t and @i <= @max
    begin
        select @c = '        {"' + col + '", "' + col + '"}, ' from @q where id = @i
        insert @out
        values (@c)
        set @i = @i + 1
        select @t = (select tbl from @q where id = @i)
    end

    insert @out
    values ('    })')
    , ('in')
    , ('    #"Renamed Columns"')
    , ('')
    , ('')
    , ('')

    set @tbl = @t
end

select *
from @out


推荐阅读