I have got data with PowerShell into a object as below, now i would like to convert it from vertical to horizontal. (Like "Text to column" function in Excel). How can I dynamically enumerate "all row 1" as an object header and the rest of them will assign to its child items?
The input file contains three columns, "Row", "Column" and "Text", in PSObject
PS > $table
Row Column Text
--- ------ ----
1 1 Dept
1 2 Time
1 3 Day1
1 4 Day2
1 5 Day3
1 6 Day4
1 7 Day5
1 8 Day6
1 9 Day7
2 1 Marketing
2 2 11:00
2 3 4
2 4 8
2 5 8
2 6 8
2 7 4
2 8 0
2 9 0
3 1 Finance
3 2 09:00
3 3 4
3 4 8
3 5 8
3 6 8
3 7 4
3 8 0
3 9 0
I tried to use the following code to assign it into another object, but I don't know how to let the content put in.
$data = New-Object –TypeName PSObject
$table | %{
if ($_.row -eq 1) {
$data | Add-Member –MemberType NoteProperty –Name $_.text –Value ''
}
}
Output:
PS > $data|ft
Dept Time Day1 Day2 Day3 Day4 Day5 Day6 Day7
---- ---- ---- ---- ---- ---- ---- ---- ----
Expected result:
Dept Time Day1 Day2 Day3 Day4 Day5 Day6 Day7
---- ---- ---- ---- ---- ---- ---- ---- ----
Marketing 11:00 4 8 8 8 4 0 0
Finance 09:00 4 8 8 8 4 0 0
There is a need for some kind of indirect reference. You can do the following:
$dataLabels = New-Object –TypeName psobject
$dataZero = New-Object –TypeName psobject
$data = #() ### or ### New-Object System.Collections.ArrayList
$table | Where-Object { $_.row -eq 1 } | ForEach-Object {
$dataLabels |
Add-Member –MemberType NoteProperty –Name $_.Column –Value $_.Text
$dataZero |
Add-Member –MemberType NoteProperty –Name $_.Text –Value ''
}
$dataZeroCsv = $dataZero | ConvertTo-Csv -NoTypeInformation
for ( $i=0;$i -lt ($Table.Row|Measure-Object -Maximum).Maximum -1;$i++)
{
$dataLine = $dataZeroCsv | ConvertFrom-Csv
$table | Where-Object { ($_.Row - 2) -eq $i } |
ForEach-Object {
$dataLine.($dataLabels.($_.Column)) = $_.Text ### indirect reference
}
$data += $dataLine ### or ### [void]$data.Add( $dataLine )
}
## Output:
$data | Format-Table
provided that $table is defined as follows:
$tableArr = #"
Row Column Text
1 1 Dept
1 2 Time
1 3 Day1
1 4 Day2
1 5 Day3
1 6 Day4
1 7 Day5
1 8 Day6
1 9 Day7
2 1 Marketing
2 2 11:00
2 3 4
2 4 8
2 5 8
2 6 8
2 7 4
2 8 0
2 9 0
3 1 Finance
3 2 09:00
3 3 3
3 4 4
3 5 5
3 6 6
3 7 7
3 8 0
3 9 1
4 1 TestDept
4 2 16:00
4 4 5
"# -split [System.Environment]::NewLine
$table = $tableArr -replace '\s+', ' ' -replace '^\s', '' |
ConvertFrom-Csv -Delimiter ' '
Output:
PS D:\PShell> D:\PShell\SO\52804963.ps1
Dept Time Day1 Day2 Day3 Day4 Day5 Day6 Day7
---- ---- ---- ---- ---- ---- ---- ---- ----
Marketing 11:00 4 8 8 8 4 0 0
Finance 09:00 3 4 5 6 7 0 1
TestDept 16:00 5
Related
Is there anyway in KDB to get the average of each row which has list data type ?
E.g. I have table with column ID and Size :
| ID | size |
|----|-----------|
| 1 | [1,3] |
| 2 | [3,3,3] |
| 3 | [4,2,4,2] |
In select , I want ID and avg of size :
| ID | avg |
|----|-----|
| 1 | 2 |
| 2 | 3 |
| 3 | 3 |
q)t:([]id:1 2 3;size:(1 3;3 3 3;4 2 4 2))
q)t
id size
----------
1 1 3
2 3 3 3
3 4 2 4 2
q)select id,avg each size from t
id size
-------
1 2
2 3
3 3
I need to compare two columns in the datatable in Powershell. As result it should create 3 new datatables:
the same values in Column 1 and Column 2
only the values in Column 1
only the values in Column 2
Values are string values.
Column 1 | Column 2
---------|----------
Value 1 | Value 3
Value 2 | Value 4
Value 3 | Value 6
Value 4 | Value 7
Value 5 |
datatable_1:
Column 3
---------
Value 3
value 4
datatable_2:
Column 1
---------
Value 1
Value 2
Value 5
datateble_3:
Column 2
---------
Value 6
Value 7
$Table = ConvertFrom-SourceTable '
Column 1 | Column 2
---------|----------
Value 1 | Value 3
Value 2 | Value 4
Value 3 | Value 6
Value 4 | Value 7
Value 5 |'
PS C:\> $Table | Select 'Column 1' | Where {$Table.'Column 2' -Contains $_.'Column 1'}
Column 1
--------
Value 3
Value 4
PS C:\> $Table | Select 'Column 1' | Where {$Table.'Column 2' -NotContains $_.'Column 1'}
Column 1
--------
Value 1
Value 2
Value 5
PS C:\> $Table | Select 'Column 2' | Where {$Table.'Column 1' -NotContains $_.'Column 2'}
Column 2
--------
Value 6
Value 7
I have an object containing the following in PowerShell:
Date Item Total
---- ---- -----
20190505 AAA 2
20190505 BBB 1
20190514 AAA 6
20190514 BBB 1
20190514 CCC 2
20190524 AAA 5
20190524 BBB 1
20190524 CCC 1
20190524 DDD 2
I want to select the maximum total for each type of item. The result should look something like this:
Item Total
---- -----
AAA 6
BBB 1
CCC 2
DDD 2
I found a solution:
$myObject | Group-Object -Property Item | Foreach {
$_.Group | Sort Total -Descending | Select -First 1
}
I think the the question can be reworded properly please kindly edit if necessary. I've also checked other questions and answers over the internet but it didn't help.
Below is what I'm trying to achieve, basically I want the columns to display horizontally; and if they reach lets say 3 columns(name) it will start on another page. These columns have subcolumns below.
I've already tried setting the Print Order to horizontal and set the columns to 3. However its showing the unexpected output.
This my table structure(These have thousands of records). I've also tried to turn this into array but it doesnt work.
How can I achieve the ouput above on the report? If you can provide documents or links about this is really helpful. Im using Postgres and ireport 3.7.6.
date | name
------+--------
1 | Name 1
2 | Name 1
3 | Name 1
4 | Name 1
5 | Name 1
6 | Name 1
7 | Name 1
8 | Name 1
9 | Name 1
10 | Name 1
1 | Name 2
2 | Name 2
3 | Name 2
4 | Name 2
5 | Name 2
6 | Name 2
7 | Name 2
8 | Name 2
9 | Name 2
10 | Name 2
1 | Name 3
2 | Name 3
3 | Name 3
4 | Name 3
5 | Name 3
6 | Name 3
7 | Name 3
8 | Name 3
9 | Name 3
10 | Name 3
(30 rows)
I'm not really familiar with ireport, but from the PostgreSQL perspective, I believe you're looking for crosstab. Bellow an example:
(If you haven't installed the extension yet, just execute this command)
CREATE EXTENSION tablefunc
Considering the following table (I believe it's close to your structure):
CREATE TEMPORARY TABLE t (id INT, name TEXT,val INT);
And the following values ...
db=#INSERT INTO t VALUES (1,'Name1',10),
(2,'Name1',20),
(3,'Name1',80),
(1,'Name2',30),
(2,'Name2',52),
(3,'Name2',40);
db=# SELECT * FROM t;
id | name | val
----+-------+-----
1 | Name1 | 10
2 | Name1 | 20
3 | Name1 | 80
1 | Name2 | 30
2 | Name2 | 52
3 | Name2 | 40
(6 Zeilen)
... you can use crosstab to display your results horizontally:
db=# SELECT *
FROM crosstab( 'SELECT name,id,val FROM t')
AS j(name text, val1 int, val2 int, val3 int);
name | val1 | val2 | val3
-------+------+------+------
Name1 | 10 | 20 | 80
Name2 | 30 | 52 | 40
(2 Zeilen)
I have data that looks like this:
ID | timespan
------ | ------
1 | 12 days 23:45:00
2 | 02:45:00
3 | 23:45:00
4 | 10 days 03:30:00
I want to exclude every timespan which includes 23:45:00
so I that get this output
ID | timespan
------ | ------
2 | 02:45:00
4 | 10 days 03:30:00
How should I write the where clause?
with data(id, timespan) as (
values
( 1, '12 days 23:45:00'::interval),
( 2, '02:45:00'),
( 3, '23:45:00'),
( 4, '10 days 03:30:00')
)
select *
from data
where timespan::text like '%23:45:00%';