Analysis Services 多维数据集的数据整理

Data Wrangling for Analysis Services Cube

我有一个数据集如下(简化):

Fund    Field1-2012    Field1-2013    Field2-2012   Field2-2013
 FD1       x               x               x            x
 FD2       x               x               x            x

如您所见,字段中存在日期,这对大多数分析来说非常不友好。要的是下面

Fund   Year   Field1   Field2
 FD1    2012   x          x
 FD1    2013   x          x
 FD2    2012   x          x
 FD2    2013   x          x

我一直在使用 SQL 服务器集成工具来完成此操作,但无济于事。是否有我应该使用的工具或 excel 中是否有可以帮助我的东西?无法暴力破解,因为数据集很大

最佳

一种选择是使用 union all:

select fund, 2012 as year, Field1-2012 as field1, Field2-2012 as field2
from yourtable
union all
select fund, 2013 as year, Field1-2013 as field1, Field2-2013 as field2
from yourtable

您有一个 R 标签,所以这是一个 R 解决方案:

df = read.table(text = "
Fund    Field1-2012    Field1-2013    Field2-2012   Field2-2013
FD1       5               7               9            10
FD2       6               8               9            10
", header=T)

library(tidyverse)

df %>%
  gather(key, value, -Fund) %>%
  separate(key, c("type","year"), convert = T) %>%
  spread(type, value)

#   Fund year Field1 Field2
# 1  FD1 2012      5      9
# 2  FD1 2013      7     10
# 3  FD2 2012      6      9
# 4  FD2 2013      8     10

您可以使用 apply 对数据进行逆透视:

select t.Fund, tt.year, tt.Field1, tt.Field2
from table t cross apply
     ( values (2012, [Field1-2012], [Field2-2012]), 
              (2013, [Field1-2013], [Field2-2013]) 
     ) tt (year, Field1, Field2);