Not able to access the INIT variable in a secondary FOR loop - for-loop

I have the following code snippet:
TYPES: BEGIN OF ty_table_line,
idx TYPE i,
posnr TYPE i,
quan TYPE i,
END OF ty_table_line,
ty_internal_table TYPE SORTED TABLE OF ty_table_line WITH UNIQUE KEY idx.
DATA(lt_set) = VALUE ty_internal_table( ( idx = 1 posnr = 1 quan = 2 )
( idx = 2 posnr = 1 quan = 40 )
( idx = 3 posnr = 1 quan = 10 )
( idx = 4 posnr = 1 quan = 88 ) ).
DATA(lt_set_2) = VALUE ty_internal_table(
FOR i = 0 WHILE i <= 4
( LINES OF VALUE #( FOR <line> IN lt_set[]
WHERE ( idx = i + 1 )
( <line> ) ) ) ).
Here, my loop starts with i=0 and I am trying to use the value in i to filter out some values from an internal table LT_SET. What is the right approach for this error?
I tried with WHERE ( idx = i + 1 ) and WHERE ( idx <> i ), but I get this error:
The variable "I" cannot be used here.

I can't explain the reason (nothing found in the ABAP documentation) but I could reproduce and find a workaround with an additional auxiliary variable declared with LET. Tested with ABAP 7.52.
TYPES: BEGIN OF ty_table_line,
idx TYPE i,
posnr TYPE i,
quan TYPE i,
END OF ty_table_line,
ty_internal_table TYPE SORTED TABLE OF ty_table_line WITH UNIQUE KEY idx.
DATA(lt_set) = VALUE ty_internal_table( ( idx = 1 posnr = 1 quan = 2 )
( idx = 2 posnr = 1 quan = 40 )
( idx = 3 posnr = 1 quan = 10 )
( idx = 4 posnr = 1 quan = 88 ) ).
DATA(lt_set_2) = VALUE ty_internal_table(
FOR i = 0 WHILE i <= 4
LET j = i IN
( LINES OF VALUE #( FOR <line> IN lt_set[]
WHERE ( idx = j + 1 )
( <line> ) ) ) ).
ASSERT lt_set_2 = lt_set.

Related

Conditional Column in PowerBI based on Priority and Count

This is a simple request.
I have to create a column in query editor, or in table view. Whichever is easy.
Column looks like this -->
A,B,C,D,D,E,D
B,C,D,B,D,A
C,C,D,F,E,G
D,D,E,E,E,F,B
Result should be based on count of characters present, with 'A' character always taking the priority.
For instance result of the above column next to it will be
A ( A will take priority even if D has most count)
A (Even though B has most count, A will take Priority)
C ( as C has most count)
E ( as E has most count)
Result =
VAR String = COALESCE ( 'Table'[Column], "XX" )
VAR Items = SUBSTITUTE ( String, ",", "|" )
VAR Length = PATHLENGTH ( Items )
VAR T1 = GENERATESERIES ( 1, Length, 1 )
VAR T2 = ADDCOLUMNS ( T1, "#Item", PATHITEM ( Items, [Value] ) )
VAR T3 = GROUPBY ( T2, [#Item], "#Count", COUNTX ( CURRENTGROUP(), 1 ) )
VAR T4 = TOPN ( 1, T3, [#Count] )
VAR Result = MAXX ( T4, [#Item] )
RETURN
IF ( PATHCONTAINS ( Items, "A" ), "A", Result )
Blockquote

How to find the minimum path with the highest sum of edge weights?

I'm trying to implement a modified Dijkstra's algorithm to find the shortest path with highest weighted value between all nodes of an undirected graph.
My problem is that I don't know how to properly change my original Fortran code (that finds the minimum path with the lowest weight value). I would like to ask if you could help me with ideas to revise this code, mainly the subroutines find_nearest and update_mind, as well the code to run all over the nodes.
c ***************************************************************************
subroutine dijkstra (nmols, matrixS)
implicit none
include 'COMMONP.dat'
integer nmols, i, j, k1, k2, step, md, mv
logical connected(nmols)
integer, parameter :: i4_huge = 2147483647
dimension mind(nmolsp), ohd(nmolsp, nmolsp)
c
ohd(1:nmols,1:nmols) = i4_huge
c
do i = 1, nmols
ohd(i,i) = 0
end do
c
c Loading the weighted matrix
c
do 14 k1 = 1, nmols
do 15 k2 = 1, nmols
if matrixS(k1,k2).ne.0 then
ohd(k1,k2) = matrixS(k1,k2)
15 continue
14 continue
c
c carry out the algorithm
c start out with only node 1 connected to the tree.
c
do 33 node = 1, nmols
connected(1:nmols) = .false.
connected(node) = .true.
c
c initialize the minimum distance to the one-step distance
c
mind(1:nmols) = ohd(node,1:nmols)
c
c attach one more node on each interaction
do step = 1, nmols
call find_nearest (nmols, mind, connected, md, mv)
if ( mv == -1 ) then
write ( *, '(a)' ) ' '
write ( *, '(a)' ) 'DIJKSTRA_DISTANCE - Warning!'
write ( *, '(a)' ) ' Search terminated early.'
write ( *, '(a)' ) ' Graph might not be connected.'
return
end if
c
c Mark this node as connected
c
connected(mv) = .true.
c
c Having determined the minimum distance to node MV, see
c what is the highest weighted path to other nodes.
c
call update_mind ( nmols, connected, ohd, mv, mind )
end do
! Print the results.
!
write ( *, '(a)' ) ' '
write ( *, '(a)' ) ' Minimum path from node i:'
write ( *, '(a)' ) ' '
do i = 1, nmols
write ( *, '(2x,i2,2x,i2)' ) i, mind(i)
end do
33 continue
!
stop
end
c ***************************************************************************
subroutine find_nearest (nmols, mind, connected, md, mv)
implicit none
integer ( kind = 4 ) nmols
logical ( kind = 4 ) connected(nmols)
integer ( kind = 4 ) d
integer ( kind = 4 ) i
integer ( kind = 4 ), parameter :: i4_huge = 2147483647
integer ( kind = 4 ) mind(nmols)
integer ( kind = 4 ) v
d = i4_huge
v = -1
do i = 1, nmols
if ( .not. connected(i) .and. mind(i) < d ) then
d = mind(i)
v = i
end if
end do
return
end
c ***************************************************************************
subroutine update_mind ( nmols, connected, ohd, mv, mind )
implicit none
integer ( kind = 4 ) nmols
logical ( kind = 4 ) connected(nmols)
integer ( kind = 4 ) i
integer ( kind = 4 ), parameter :: i4_huge = 2147483647
integer ( kind = 4 ) mind(nmols)
integer ( kind = 4 ) mv
integer ( kind = 4 ) ohd(nmols,nmols)
do i = 1, nmols
if ( .not. connected(i) ) then
c
if ( ohd(mv,i) < i4_huge ) then
if ( mind(mv) + ohd(mv,i) < mind(i) ) then
mind(i) = mind(mv) + ohd(mv,i)
end if
end if
end if
end do
return
end
c

Calculate % based on a combination of count and stored values - Power BI

I have a basic table that looks like this:
DayNo. Customer AgentsInvolved CallID
0 AAA 1 1858
0 AAA 3 1859
2 AAA 1 1860
0 BBB 2 1862
0 CCC 1 1863
0 DDD 3 1864
9 DDD 1 1865
9 DDD 4 1866
I need to be able to find the % of customers who only contacted only once, and spoke to 1 agent only. So from the above example, out of 4 distinct customers only customer CCC falls into this category (1 call, 1 AgentInvolved)
So the Desired result would be: 1/4 or 25%
How can I create a Power BI measure to do this calc?
Try this measure:
Desired Result =
VAR summarizetable =
SUMMARIZECOLUMNS (
'table'[Customer],
"Calls", COUNT ( 'table'[CallID] ),
"Agents", SUM ( 'table'[AgentsInvolved] ),
"Day", SUM ( 'table'[DayNo.] )
)
RETURN
COUNTROWS (
FILTER ( summarizetable, [Calls] = 1 && [Agents] = 1 && [Day] = 0 )
)
/ COUNTROWS ( summarizetable )
The summarized table created on the fly in VAR summarizetable looks like this:
Here's another approach:
Measure =
SUMX(
VALUES(Table2[Customer]),
CALCULATE(
IF(
DISTINCTCOUNT(Table2[CallID]) = 1 &&
SUM(Table2[AgentsInvolved]) = 1,
1,
0
),
Table2[DayNo.] = 0
)
) /
DISTINCTCOUNT(Table2[Customer])
If you want to exclude the 0 day rows in the denominator as well, replace the last line with
CALCULATE(DISTINCTCOUNT(Table2[Customer]), Table2[DayNo.] = 0)
Desired Result =
VAR summarizetable =
SUMMARIZECOLUMNS (
'table'[AgentsInvolved],
'table'[DayNo.],
'table'[Customer],
"Calls", COUNT ( 'table'[CallID] )
)
)
RETURN
COUNTROWS (
FILTER ( summarizetable, [Calls] = 1 && 'table'[AgentsInvolved] = 1 && 'table'[DayNo.] = 0 )
) / DISTINCTCOUNT ( 'table'[Customer])

Convert to Laravel Query

I have query for query data. That is document.
"SELECT
zk_z_hako * CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
+ zk_z_bara
- ifnull(
sum(
ns_hako
* CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END
+ ns_bara
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END )
,0 ) AS TOTAL_BARA
FROM t_table1
LEFT JOIN t_table2
ON ns_kno = zk_kno
AND ns_show_flg = 0
AND ns_ymd > 'Date param'
WHERE zk_kno = Value param;
So I am not a master of Laravel. Now I need to convert this query for work with laravel. Anyone can help me?
And i have to try this query.
$squery = 'zk_z_hako * CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
+ zk_z_bara
ifnull(
sum(
ns_hako
* CASE WHEN zk_n_iri> 0 THEN zk_n_iri ELSE 1 END
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END
+ ns_bara
* CASE WHEN ns_tr_kbn in (0,6) OR ( ns_tr_kbn = 1 AND ns_ns_kbn = 7) THEN 1
WHEN ns_tr_kbn in (1,7) THEN (-1)
ELSE 0
END )
,0 ) AS TOTAL_BARA ';
$param1= '20160310';
$param2= '1972640100';
$results = DB::table('table1')
->select($squery)
->leftJoin('table2', function($join) use ($param1)
{
$join->on('table1.ns_kno', '=', 'table2.zk_kno');
$join->on('table1.ns_show_flg', '=', DB::raw(0));
$join->on('ns_ymd','>',DB::raw("'".$param1."'"));
})
->where('zk_kno', DB::raw($param2))
->toSql()
But it's return sql
"select `zk_z_hako` as `CASE` from `t_zaikmst` left join `t_nsyutrn` on `t_nsyutrn`.`ns_kno` = `t_zaikmst`.`zk_kno` and `t_nsyutrn`.`ns_show_flg` = 0 and `ns_ymd` > '20160310' where `zk_kno` = 1972640100"
i don't sure it true.
If you want to make custom select than you need to use raw queries as select parameter like this:
->select(\DB::raw($squery))

Give each object in scene a unique wirecolor?

Know of any scripts, or code, to do this?
I've seen several which gives random colors, but I need to make sure no two objects have the same wirecolor..
Thanks! =)
I don't know any but it's not so hard to write one.
Here you go, hope it will do what you expected it to:
fn shuffle &arr =
(
local temp, swapIndex, counter = arr.count + 1
while counter > 1 do
(
swapIndex = random 1 (counter -= 1)
temp = arr[counter]
arr[counter] = arr[swapIndex]
arr[swapIndex] = temp
)
OK
)
fn incrementCounters &r &g &b step =
(
if (b += step) > 256 do
(
b = 1
if (g += step) > 256 do
(
g = 1
if (r += step) > 256 do r = 1
)
)
)
fn assignRandomWirecolor objs simple:true =
(
local stepCount = objs.count^(double 1/3) + 1
local step = 255./stepCount
local redArr = #(0) + #{1..255}
local greenArr = copy redArr #noMap
local blueArr = copy redArr #noMap
local r = local g = local b = 1
if simple then
(
shuffle &redArr
shuffle &greenArr
shuffle &blueArr
)
else shuffle &sel -- slower with many objects
for obj in objs do
(
obj.wirecolor = [redArr[int(r)], greenArr[int(g)], blueArr[int(b)]]
incrementCounters &r &g &b step
)
)
sel = selection as array
clearSelection()
assignRandomWirecolor sel --simple:false --> if simple is not so cool, try the other option
select sel
Of course it all also depends on the purpose you want to use it for, this is just a general approach and as such it might not be suitable for that exact task. If that's the case, you can give more details and I'll make some adjustments.

Resources